Programming :: Split Fields In A Perl Array?
Dec 16, 2010
I have an array called @logons. How can I step thru the array and split the fields? This is what I have so far, but doesnt work. I got the feeling I the split statement syntax is incorrect.
Code:
print @logons;
foreach my $logons(@logons){
($userid, $ip) = split(',',$logons);
[code]....
Update: Appears the data in @logons has a column header from the mysql query which I used to populate it with. So that code which I was testing does indeed work.
View 10 Replies
ADVERTISEMENT
May 17, 2010
I am trying to split a pattern in perl and so far I cannot get it to work. I have a pattern that looks like this:
76|2455311|2455312|00:00:00|00:00:00|Once|0|Donkey | |
I always need to print out the 8th field. So in this case donkey. Here is what I have so far:
Code:
#!/usr/bin/perl
use strict;
use warnings;
[Code].....
View 6 Replies
View Related
Apr 14, 2011
I'm currently teaching myself perl. I'm trying to write a program that unscrambles words by comparing each element in the array. There is an input file where you type the words to be unscrambled and there is a word list file containing approximately 1300 words. After everything has been processed the program is supposed to paste those words to an output file.
Code:
#!/usr/local/bin/perl
#use strict; #always add this in the beginning of code.
#use warnings; #always add this in the beginning of code.
#open(INFILE, '<', 'sample.txt'); # For reading
#open(INFILE, '>', 'sample.txt'); # For writing
#open(INFILE, '>>', 'sample.txt'); # To append .....
View 4 Replies
View Related
Sep 18, 2010
I have a file (called twitterstatus.tmp) that looks like this:
Code:
<status>
<id>24854489768</id>
<text>Are we gonna ride the sun home?</text>
<id>55266987</id>
[code].....
How could I feed this into an array, with each element containing everything between the <status> </status> tags?
View 9 Replies
View Related
Jun 4, 2010
How can i split a string like this,
Code:
This is my first line.
This is my second line.
This is my third line.
into,
Code:
"This is my first line.", "This is my second line.", "This is my third line."
in C++.
i.e. split the code at every new line
View 11 Replies
View Related
Apr 27, 2010
created this array and the last two elements will not pop - phil and sylvia never get up to bat.
1#!/usr/bin/perl
2use strict;
3use warnings;
[code]...
View 3 Replies
View Related
Nov 28, 2008
To do this, can I do this?:
if (@ary = ""),
or is something more needed?
View 4 Replies
View Related
Dec 14, 2010
How do I pass an array from one perl script to another .. also how to read it in another script ?I tried below -
Script 1 - sender program
system ("./program2.pl @array1 @array2");
printf("
[code]...
View 2 Replies
View Related
Apr 22, 2011
I tried to look for this, but there are so little of perl on internet:
Code:
@array = [$title, $description, $pubDate, $link];
push(@feeds1, $array);
I am trying to create a 2 D array by PUSH, so the output would look like:
([ $title, $description, $pubDate, $link ], [ $title, $description, $pubDate, $link ], [ $title, $description, $pubDate, $link ], [ $title, $description, $pubDate, $link ]...etc )
View 2 Replies
View Related
Mar 13, 2011
I have .txt.gz files that store queries made on a browser, d my job is to analyze them.The information is stored in a xml-like style.Quote:
<browser>lwp-trivial/1.41</browser>
<http_code>200</http_code>
<keywords />
[code]...
View 6 Replies
View Related
Nov 25, 2010
I am trying to execute a Unix Command in perl and assigning its output to an array:
Code:
@File_List=exec("ls -1 /tmp");
but it is not working. I have tried the perl function system() also but its return code is
[code]...
View 10 Replies
View Related
May 10, 2010
Exist it a perl function to count how much each array element is into array?
View 14 Replies
View Related
Feb 9, 2010
How to restrict the maximum size of an array variable in perl such that Ishould not be able to have array elements beyond the maximum size?
View 7 Replies
View Related
Dec 7, 2010
I have data that looks similar to this -
Quote:
John Smith (Address)
123 Main St
Unit 1
[code].....
So I need everything between each name, but I am not guaranteed that each time I match a name that I will have the same amount of lines, so I do a range pattern search line this to get all lines, no matter if there is 5 or 10 or 15. I simply do a loop that goes through the whole array until I hit the match, and this is my search pattern.
Code:
if ($LINE =~ /^s+$SELECTED_NAME/ ... $LINE =~ /(Address)/) {
push @ADDRESS_INFO, $LINE;
}
This works perfectly... until I hit the end and it doesn't get its final pattern match because it's at the end and there is no next entry with a (Address) line. So as a 'hack', I ended up inserting a final scalar at the end of the array that just says (Address) so it knows it's at the end. Ideally though, I'd like to do an "or" statement that says search for Address || return true if I hit the end of the array. How would I match on "End Of Array" essentially?
View 1 Replies
View Related
Jul 7, 2010
I am in need of some syntax help. I'm trying to figure out how to store and retrieve an array out of a hash of hashes. For this example, I'm trying to access the city list for a particular state for a particular country. I understand I could do a join and split on the hash key to combine Country and State, but trying to keep things separated.The code I have gets in all the information for the Countries, and states, and gets the list of cities together, no problem. I store all the city names in an array, then make an anonymous pointer to the array for the hash, like this - $MY_CITIES{$COUNTRY}{$STATE} = [@CITIES]I believe that syntax is correct, or is it? What I'd like to do is I need to cycle through every hash to find if a city exists or not. If it exists in 3 states, then it should print 3 times. Here is the code block to search -
Code:
for $COUNTRY (@LIST_OF_COUNTRIES) {
for $STATE ( keys %MY_CITIES{$COUNTRY){$STATE} ) {
[code]...
View 4 Replies
View Related
Aug 21, 2010
it's been a while since I logged on here! I've been trying my hand at a little perl and have hit a brick wall.I'm using the Imagemagick module to manipulate some images. I can get the following to work without issue:
Code:
$teampath = "/var/www/team1";
$player=Image::Magick->new;
[code]...
View 1 Replies
View Related
Jul 22, 2011
I'm reading "OReilly Learning Perl 5th Edition", and there are such words:Code:You can use an array element like $fred[2] in every place? where you could use any other scalavariable like $fred.At the bottom of the page, it explains the ? like this:Code:The most notable exception is that the control variable of a foreach loop, which you?ll see later in this chapter, must be a simple scalar.Since Perl has the save-and-restore mechanism for the control variable, why an array element can't be used as the control variable
View 1 Replies
View Related
Feb 27, 2011
I have a single file that contain multi-text something likes this:
Quote:
No. Time Source Destination Protocol Info
185 27712.068199 192.168.18.23 192.168.18.191 SMTP S: 250 2.1.5 Ok
No. Time Source Destination Protocol Info
186 27715.068293 192.168.0.50 192.168.5.2 TCP suncacao-jmxmp > 44693 [ACK] Seq=1 Ack=1 Win=64807 Len=1380
[Code].....
View 14 Replies
View Related
Mar 17, 2011
how do I split a string into an array?In this string:"this is a story"how do I split it by the space?
View 8 Replies
View Related
Aug 8, 2009
The below snippet works fine until I use strict. Then it dies with the following error: uote:Can't use string ("html") as an ARRAY ref while "strict refs" in use at ./filetest3 line 18.I want to create @lists based on the $scalars in @type. However, "my @$ext = ()"; and push (@$ext, @files); do not play nice with strict. How do I get around this?Quote:
#!/usr/bin/perl -w
system clear;
use strict;
[code]...
View 9 Replies
View Related
Mar 21, 2011
I'm trying to split a string, to later iterate using a for loop like
Code:
for (( i=0; i<5; i++))
But, my script returns an array with the size 1.
Here's the script:
Code:
aver=$(grep "avg" A.txt | awk '{ print $2 }');
a=$(echo $aver | tr " " "
");
[Code]....
View 2 Replies
View Related
Apr 27, 2010
I have a filename file-name-here-1.2.3-i486-2.tar I would like to know how to remove the last two fields leaving only: file-name-here-1.2.3
I can use:
x=file-name-here-1.2.3-i486-2.tar
echo ${a%-*}
to remove the last field (after -) but how do i remove the last two in one fell swoop?
View 2 Replies
View Related
Mar 6, 2011
I am trying to dynamically delete and add information into the array "blah"
Code:
int blahsize = 1;
char** blah = (char**) calloc(blahsize+1,sizeof(char*));
Adding information:
Code:
blah[1]=stuff1;
blah[2]=stuff2;
code....
View 2 Replies
View Related
Oct 8, 2010
I have a file records as follows:
Jane pepe@pepe.biz
john@pepe.net John
Joe joe@willxyz.org
How can I get that always first field be given name and second one be the mail address?
I've tried
Code:
awk '$1 ~ /@/ { a=$1 ; $1=$2; $2=a; print }' file
But that doesn't worked
View 5 Replies
View Related
Jun 7, 2010
I have trouble converting a short array to a char array
Code:
short pShort[4] = { 0x41, 0x42, 0x43, 0x44 };
How to convert this to a char array?
View 4 Replies
View Related
May 31, 2010
I want to extract all the common entries in the given 3 columns(fields) in the file using awk::can somebody tell me how it can be done in awk...the file is::
NC_000858.pttNC_000858.fnaNC_001403.rnt
NC_001362.pttNC_001436.fnaNC_001407.rnt
NC_001364.pttNC_001503.fnaNC_001488.rnt
[code]....
View 8 Replies
View Related
Jul 16, 2011
Dear expertsI have files like
ABD : 5869 events, relative ratio : 1.173800E-01 , sum of ratios : 1.173800E-01
VBD : 12147 events, relative ratio : 2.429400E-01 , sum of ratios : 3.603200E-01
SDF : 17000 events, relative ratio : 3.400000E-01 , sum of ratios : 7.003200E-01
[code]....
View 3 Replies
View Related
Mar 2, 2011
I have a file with 200 000 lines and I want to append the fields of each line based on matching first field. The resulting file should have 70 000 columns but has "only" 18 000. The command I'm using is working perfectly with a smaller file, wich lead to 14 000 columns. Could there be a limit in number of fields that awk can handle ? Here's my awk command :
Code:
awk -F, 'END { for (k in _) print _[k] } { _[$1] = $1 in _ ? _[$1] FS $4 : $1","$4 } ' file > out
Also, this command writes ^M (windows line break) after each columns. Removing them is easy but where do they come from ? Working on Ubuntu 10.10
View 4 Replies
View Related
Apr 9, 2009
I want to write a function which calculates the space needed between fields, to generate a table with aligned fields, like when you type "ls -l", the operating system generates a table with beautifully aligned fields. I've got this code so far:
Code:
for line in $(cat tmpSearch)
do
line=`echo $line | tr ":" " "`
[code]....
View 2 Replies
View Related
Mar 25, 2010
i am reading a database flat file with the "awk" command which has 4 fields separated by colon ":" .I want to show the output of these fields in a certain way that every record is numbered e.g
1.some text
2.some text
Is there a way to do this?
View 14 Replies
View Related