How to ignore 1st line in a file when reading

D

Deepu

Hi all,

I would like to know when reading a file which has around 10 - 20 lines
in it. How can it be done to ignore 1st line and start reading from 2nd
till last.

Example:

FILE0 ## the name will not be same in all the other files ##
FILE1
FILE2
FILE3
FILE4
FILE5

Now i need to read from 2nd line. Can somebody please help me on this.

To read the whole file, i just use:

open (FH, "FileName") || die "Can't open";

while (<FH>) {
push (@array, $_);
}

Thanks
Deep
 
B

Brian Wakem

Deepu said:
Hi all,

I would like to know when reading a file which has around 10 - 20 lines
in it. How can it be done to ignore 1st line and start reading from 2nd
till last.

Example:

FILE0 ## the name will not be same in all the other files ##
FILE1
FILE2
FILE3
FILE4
FILE5

Now i need to read from 2nd line. Can somebody please help me on this.

To read the whole file, i just use:

open (FH, "FileName") || die "Can't open";

while (<FH>) {
push (@array, $_);
}

Thanks
Deep


If you are reading into an array then your question may as well be about
skipping the first element of an array.


my @array = qw( first second third fourth fifth );
print "$_\n" foreach @array[1..$#array];


second
third
fourth
fifth
 
L

l v

Deepu said:
Hi all,

I would like to know when reading a file which has around 10 - 20 lines
in it. How can it be done to ignore 1st line and start reading from 2nd
till last.

Example:

FILE0 ## the name will not be same in all the other files ##
FILE1
FILE2
FILE3
FILE4
FILE5

Now i need to read from 2nd line. Can somebody please help me on this.

To read the whole file, i just use:

open (FH, "FileName") || die "Can't open";

while (<FH>) { next if ($. == 1); # <------
push (@array, $_);
}

Thanks
Deep

use the variable $. as I've shown above.

see perldoc perlvar for more information regarding $.
 
D

Deepu

Sorry for my incorrect statement.

Brian said:
If you are reading into an array then your question may as well be about
skipping the first element of an array.

I am not reading into an array.

basically i will take each line and compare with a pattern.

while (<FH>){

if ($_ =~ /^\s*(\S+)\t+pg:\s+(\S+)/) { ## here i need to start from 2nd
line
## Do something ##
}

}


Thanks for helping me out.
my @array = qw( first second third fourth fifth );
print "$_\n" foreach @array[1..$#array];


second
third
fourth
fifth
 
I

it_says_BALLS_on_your forehead

Deepu said:
Sorry for my incorrect statement.

I would do the following, so I don't have to perform a check inside the
loop for every iteration:

open my $fh, '<', $file or die "can't open $file: $!\n";
chomp( my $first_line = <$fh> );

while ( <$fh> ) {
# do whatever.
}

close $fh or die "can't close $file: $!\n";
HTH.
 
D

Dr.Ruud

Deepu schreef:
I would like to know when reading a file which has around 10 - 20
lines in it. How can it be done to ignore 1st line and start reading
from 2nd till last.
[...]
open (FH, "FileName") || die "Can't open";

while (<FH>) {
push (@array, $_);
}

You could use:

push @array, $_ if ($. > 1) ;


Variant:

#!/usr/bin/perl
use strict ;
use warnings ;

my $filename = q[FileName] ;
open my $fh, q[<], $filename or die qq[Cannot open $filename: $!] ;
@array = <$fh> ;
shift @array ;
 
T

Tad McClellan

Deepu said:
Now i need to read from 2nd line. Can somebody please help me on this.

To read the whole file, i just use:

open (FH, "FileName") || die "Can't open";


while (<FH>) {
push (@array, $_);
}
 
M

Mumia W.

Tim said:
Here's my solution, very similar to another poster's:

open FH, "<filename" or die "can't open: $!";
<FH>; # discards first line [...]

No, it probably discards the file. In a list context (the default), <FH>
returns a list of all the the remaining lines in the file. To get a
single line, do this:

scalar <FH>;


HTH
 
D

Dodger

Deepu said:
Sorry for my incorrect statement.



I am not reading into an array.

basically i will take each line and compare with a pattern.

It's pretty simple to just skip a line...

my $skipped = 0;
while (<FILE>) {
unless ($skipped) {
$skipped = 1;
next;
}
# do something with the line
}
 
D

Dodger

Jim said:
Mumia said:
Tim said:
Here's my solution, very similar to another poster's:

open FH, "<filename" or die "can't open: $!";
<FH>; # discards first line [...]

No, it probably discards the file. In a list context (the default), <FH>
returns a list of all the the remaining lines in the file. To get a
single line, do this:

scalar <FH>;

Jim and Tim are right. Mumia is half right.

Yes, in a list context <FH> would discard the whole file.
However, on a line by itself, <FH> is not in list context. It's in
scalar context and thus just pulls one line, just like when it's in the
well-known while (<FH>) {} block (as the () on a while indicate a
conditional, not a list context).

I haven't tested it, but I'd be more wary of the possibility of Mumia's
suggestion doing what Mumia wants to avoid. Since scalar() explicitly
casts something into scalar context, I'd be concerned that it
implicitly treats it in a list context to do so, which, if the case,
would have the paradoxical effect of slupring the whole file and
returning the number of lines in it. Then again, the whole 'DWIM'
philosophy might mean that such a case is checked for beforehand behind
the scenes and that situation avoided. Again, I'm not sure on that one
either way. I'd have to try it, and I'm not gonna right now. But it
would have me more wary than just the implicitly scalar <FH> by itself,
as statements are in scalar context unless stated otherwise.
 
J

John W. Krahn

Dodger said:
Jim said:
Mumia said:
Tim Hammerquist wrote:
Here's my solution, very similar to another poster's:

open FH, "<filename" or die "can't open: $!";
<FH>; # discards first line [...]
No, it probably discards the file. In a list context (the default), <FH>
returns a list of all the the remaining lines in the file. To get a
single line, do this:

scalar <FH>;

Jim and Tim are right. Mumia is half right.

Yes, in a list context <FH> would discard the whole file.
However, on a line by itself, <FH> is not in list context. It's in
scalar context and thus just pulls one line,

Actually it is in void context, which in this case behaves the same as scalar
context.


John
 
M

Mumia W.

Jim said:
Mumia said:
Tim said:
Here's my solution, very similar to another poster's:

open FH, "<filename" or die "can't open: $!";
<FH>; # discards first line [...]
No, it probably discards the file. In a list context (the default), <FH>
returns a list of all the the remaining lines in the file. To get a
single line, do this:

scalar <FH>;


Easy to test:
[...]

It looks like I was wrong. I had a program where that happened, and I
concluded that <FH> by itself was read in list context. I used this line :

print <FH>;

And I got the rest of the file. But <FH> by itself works fine.
 
D

Dodger

Mumia said:
It looks like I was wrong. I had a program where that happened, and I
concluded that <FH> by itself was read in list context. I used this line :
print <FH>;
And I got the rest of the file. But <FH> by itself works fine.

Yup. Print treats its arguments as list context (except the filehandle
if present) and thus would do that. That's why if you print an array
you get the whole array (joined by the output record seperator, which
is undef by default), not the number of items in the array.

my @ary = qw(a b c d e f);
my $sc_ary = @ary;
print $sc_ary, @ary;

# prints 6abcdef
 
B

Ben Morrow

Quoth "Dodger said:
Jim said:
Mumia said:
Tim Hammerquist wrote:

Here's my solution, very similar to another poster's:

open FH, "<filename" or die "can't open: $!";
<FH>; # discards first line [...]

No, it probably discards the file. In a list context (the default), <FH>
returns a list of all the the remaining lines in the file. To get a
single line, do this:

scalar <FH>;

Jim and Tim are right. Mumia is half right.

No, Mumia is wrong.
Yes, in a list context <FH> would discard the whole file.
However, on a line by itself, <FH> is not in list context. It's in
scalar context and thus just pulls one line, just like when it's in the
well-known while (<FH>) {} block (as the () on a while indicate a
conditional, not a list context).

I haven't tested it, but I'd be more wary of the possibility of Mumia's
suggestion doing what Mumia wants to avoid. Since scalar() explicitly
casts something into scalar context, I'd be concerned that it
implicitly treats it in a list context to do so,

*Please* don't talk nonsense about things you don't understand. scalar
evaluates its argument in scalar context, and returns the result. End of
story.
which, if the case,
would have the paradoxical effect of slupring the whole file and
returning the number of lines in it.

No, 'cos the only builtin that returns its size in scalar context is an
array. The effect of attempting to evaluate a random list in scalar
context is to return just the last value.
Then again, the whole 'DWIM' philosophy might mean that such a case is
checked for beforehand behind the scenes and that situation avoided.

No, no DWIM here. Just rules being followed.
Again, I'm not sure on that one
either way. I'd have to try it, and I'm not gonna right now. But it
would have me more wary than just the implicitly scalar <FH> by itself,
as statements are in scalar context unless stated otherwise.

Expressions are in void context unless on the RHS of an assignment.

Ben
 
B

Brian McCauley

John said:
Dodger said:
Jim said:
Tim Hammerquist wrote:
<FH>; # discards first line [...]
No, it probably discards the file. In a list context (the default), <FH>
returns a list of all the the remaining lines in the file. To get a
single line, do this:

scalar <FH>;

Jim and Tim are right. Mumia is half right.

Yes, in a list context <FH> would discard the whole file.
However, on a line by itself, <FH> is not in list context. It's in
scalar context and thus just pulls one line,

Actually it is in void context, which in this case behaves the same as scalar
context.

Actually <FH> (or any expression) as a statement by itself is in a void
context if unless it happens to be the last statement in a block.

If it is the last statement in a block it's in whatever context the
block is being evaluated in. Very often this will still be the void
context.

I generally recommend explicitly forcing a scalar context on <> in this
case even though 99.9% of the time it is redundant.
 
J

John W. Krahn

Ben said:
Expressions are in void context unless on the RHS of an assignment.

Or on the RHS of an operator or on the RHS of a statement modifier...

$ perl -le'
sub test { print defined wantarray ? wantarray ? "LIST" : "SCALAR" : "VOID" }
test;
scalar test;
grep 1, test;
map 1, test;
1 if test;
ord test;
rand test;
'
VOID
SCALAR
LIST
LIST
SCALAR
SCALAR
SCALAR



John
 
A

anno4000

[printing an array]
Yup. Print treats its arguments as list context (except the filehandle
if present) and thus would do that. That's why if you print an array
you get the whole array (joined by the output record seperator, which
is undef by default), not the number of items in the array.

Imprecise.

s/output record separator/output field separator/;
s/undef/an empty string/;

Anno
 
A

anno4000

Dr.Ruud said:
Deepu schreef:
I would like to know when reading a file which has around 10 - 20
lines in it. How can it be done to ignore 1st line and start reading
from 2nd till last.
[...]

@array = <$fh> ;
shift @array ;

( undef, @array) = <$fh>;

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top