I am only interested in the last set of digits on the second line. My plan
was to split on coma and take the 3 element of the array. I am having problems
skipping the fist line. Any assistance would be appreciated.
Chuck Belcher
***********************************************************************
The opinion(s) expressed in this e-mail message are those of the sender
alone and do not represent the position of Mercantile Bankshares
Corporation or its Affiliates unless specifically so stated herein.
Additionally, the information contained in this message is intended
only for the persons to whom it is addressed and may contain
confidential or privileged material. Copying, distributing,
dissemination, reliance on, or other use of the information by persons
other than the intended recipient(s) is prohibited. If you received
this message in error, please notify the sender and delete the entire
message from any computer.
***********************************************************************
> -----Original Message-----
> From: Kipp, James
> Sent: Thursday, October 31, 2002 12:32 PM
> To: 'Charles...@mercantile.net'; begi...@perl.org
> Subject: RE: skip the first line
>
>
>
> open (F, "$file") or die "can't open $!\n";
> while ($line = <IN>) {
> next if $. == 0; # first line is skipped
> # do stuff with next line
> > --
> > To unsubscribe, e-mail: beginners-...@perl.org
> > For additional commands, e-mail: beginne...@perl.org
> >
> >
>
It looks like valid data is three fields separated by commas so either
of these should work:
while ( <FILE> ) {
next unless tr/,// == 2;
my @fields = split /,/;
while ( <FILE> ) {
my @fields = split /,/;
next unless @fields == 3;
while ( <FILE> ) {
next unless @fields = /^(\d+),(\d+),(\d+)$/;
John
--
use Perl;
program
fulfillment
open(INFILE,"myfile.txt") || die "Could not open myfile.txt! $!";
<INFILE>; #skip the first line
$_ = <INFILE>;
my $thirdField = (split /,/,$_)[2]; #Get the third elementof
#the array created by splitting
-----Original Message-----
From: Charles...@mercantile.net
[mailto:Charles...@mercantile.net]
Sent: Thursday, October 31, 2002 9:22 AM
To: begi...@perl.org
Subject: skip the first line
I have a file that looks like
0
232,32387,2323
I am only interested in the last set of digits on the second line. My plan
was to split on coma and take the 3 element of the array. I am having
problems
skipping the fist line. Any assistance would be appreciated.
At 12:22 PM 10/31/2002 -0500, Charles...@mercantile.net wrote:
>
>I have a file that looks like
>0
>232,32387,2323
>
>I am only interested in the last set of digits on the second line. My plan
>was to split on coma and take the 3 element of the array. I am having
>problems skipping the fist line. Any assistance would be appreciated.
Let's assume you have the data in a file called foo:
# perl -MEnglish -F"," -ane 'chomp @F;
print $F[2], "\n" if $NR > 1 and $#F' foo
Explanation of options:
-MEnglish = loads the English module; needed for $NR (record number variable)
-F = specify the regular expression to user for autosplit (-a)
-a = autosplit the record (line) into array @F (makes perl work like awk)
-n = read each line and apply script (makes perl work like awk)
-e = read following command line argument as a script
Explanation of script:
remove the newline from array F [ chomp @F ]
if split worked and the record number is greater than 1 [ if $NR > 1 ] then
print the third item [ print $F[2], "\n" ]
This one-liner will work on multiple files including stdin.
'perldoc perlrun' for more details.
Regards,
- Robert