ozboomer has asked for the wisdom of the Perl Monks concerning the following question:
Now, I realize I could probably use a (fancy) regex... and I understand they are (often) efficient and so on... but, like a lot of people, I get intimidated by regex... so I was looking for a simpler, array-manipulation method of extracting the last item from a variable length array.
Some example code:
There are a number of solutions there that I've worked-out... so I've more-or-less come up with a suitable solution... but I guess the primary question is why doesn't the following construct work:# $rec = " 2425 S 25 \" 6 47! 86 18 21! 87 23 23! - + -! - -! 96"; use Data::Dumper; $infile = "2425-pmpk.txt"; open(INFILE, $infile) || die("cant on $infile\n$!\n"); while ($rec = <INFILE>) { chomp($rec); next unless ($rec =~ /S 25/); $rec =~ s/^\s+//; # remove lead/trail spaces $rec =~ s/\s+$//; # DOESN'T WORK (@junk, $ads) = (split(/\s+/, $rec)); # WORKS OK # (@junk) = (split(/\s+/, $rec)); # $ads = @junk[-1]; # WORKS OK # (@junk) = (split(/\s+/, $rec)); # $idx = $#junk; # $ads = $junk[$idx]; # WORKS OK ... BUT AT THE WRONG END OF THE ARRAY # ($ads, @junk) = (split(/\s+/, $rec)); print Dumper(@junk); printf("$rec\n"); printf("%d\n", $ads); } close(INFILE);
(@junk, $ads) = (split(/\s+/, $rec));
I've looked through the perlfaq, the Camel and Ram books... and have searched through the HallowedHalls(tm) here but couldn't find anything specific..
Would appreciate any thoughts...
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Non-regex Methods of Extracting the Last Field from a Record
by toolic (Bishop) on May 08, 2009 at 00:13 UTC | |
Re: Non-regex Methods of Extracting the Last Field from a Record
by AnomalousMonk (Archbishop) on May 08, 2009 at 00:20 UTC | |
Re: Non-regex Methods of Extracting the Last Field from a Record
by ELISHEVA (Prior) on May 08, 2009 at 04:07 UTC | |
Re: Non-regex Methods of Extracting the Last Field from a Record
by blokhead (Monsignor) on May 08, 2009 at 01:35 UTC | |
Re: Non-regex Methods of Extracting the Last Field from a Record
by codeacrobat (Chaplain) on May 08, 2009 at 06:08 UTC | |
Re: Non-regex Methods of Extracting the Last Field from a Record
by allolex (Curate) on May 08, 2009 at 17:12 UTC |