Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

skip the first line

1,589 views
Skip to first unread message

Charles Belcher

unread,
Oct 31, 2002, 12:22:00 PM10/31/02
to begi...@perl.org

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.


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.
***********************************************************************


James Kipp

unread,
Oct 31, 2002, 12:39:25 PM10/31/02
to Charles...@mercantile.net, begi...@perl.org
Correctition: record number should be 1 not 0
try this version:
open (F, "$file") or die "can't open $!\n";
while ($line = <F>) {
next if $. == 1; # first line is skipped
# do stuff with next line
}

> -----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
> >
> >
>

James Kipp

unread,
Oct 31, 2002, 12:32:01 PM10/31/02
to Charles...@mercantile.net, begi...@perl.org

Neil Dolling

unread,
Oct 31, 2002, 1:06:30 PM10/31/02
to begi...@perl.org
open(FH,"your_file.txt") || die $!;
my @tmp = readline(*FH); #this will read each line in your file
my @second_row = split(/,/,join('',$tmp[1]),"\n"); #just look at the
second row
print($second_row[2]); #third element in array
close(FH);

John W. Krahn

unread,
Nov 1, 2002, 3:00:45 PM11/1/02
to begi...@perl.org
Charles Belcher 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.


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

Timothy Johnson

unread,
Nov 1, 2002, 3:13:34 PM11/1/02
to Charles...@mercantile.net, begi...@perl.org

Maybe I'm reading this wrong, but if you're only looking for the third field
on the second line, why not just do this:

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.

Robert Citek

unread,
Nov 1, 2002, 4:36:49 PM11/1/02
to Charles...@mercantile.net, begi...@perl.org

Hello Charles,

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

0 new messages