Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: reading file into an array

by McDarren (Abbot)
on Dec 05, 2008 at 21:03 UTC ( #728382=note: print w/replies, xml ) Need Help??

in reply to reading file into an array

for(my $x=14; $x<@fileContents; $x++)
Array indexes in Perl begin with zero, so that's actually going to start at line 15.

Also, a more perlish way of initiating that loop would be:

for (13 .. $#fileContents) { print "$fileContents[$_]\n"; # Or whatever }
However, a much better way to process a file line by line is with a while loop. Something like this:
my $lines_to_skip = 14; while (my $line = <$file>) { # $. will give you the current line number of the file next if ($. <= $lines_to_skip); chomp($line); # Do stuff with $line }

Hope this helps,
Darren :)

Replies are listed 'Best First'.
Re^2: reading file into an array
by johngg (Abbot) on Dec 05, 2008 at 22:16 UTC

    Processing line by line in a while loop is a a more scaleable solution but I would prefer to separate the line skipping part from the main line processing logic. I think it keeps things clearer but others may disagree.

    my $linesToSkip = 14; my $discard = <$file> for 1 .. $linesToSkip; while( my $line = <$file> ) { chomp $line; ... }

    Calling the scalar $discard makes it clear that it is something we are not interested in whereas $line might be something we want to work with.

    I hope you find these thoughts of interest.



Re^2: reading file into an array
by JadeNB (Chaplain) on Dec 05, 2008 at 23:59 UTC
    I can't help mentioning, because I was so happy when I stumbled across it, the following cute syntax for skipping a constant number of lines using the range operator:
    while ( my $line = <$file> ) { next if 1 .. 14; do_stuff; }
    (Unfortunately, it doesn't work with a variable—for example, next if 1 .. $lines_to_skip—since the range operator just tests whether $lines_to_skip is a true value, not whether it's equal to $..)
Re^2: reading file into an array
by vassago (Novice) on Dec 05, 2008 at 21:26 UTC
    Sorry, it starts on line 15. Thanks for the reply. It is working now. And I like your way of the while loop much better than what I used. However, I'm new to perl and don't fully understand your for loop. I assume the "13 .." just means start at 14 but why is there a '#' in front of the fileContents and why $ instead of @? Java was my first language so perl is very different and its taking a lot of getting used to. Thanks for the replies!
      $# (not #$) refers to the index of the last element of an array.

      The "at-sign" sigil (@) is used when you refer to two or more elements of an array. If you are accessing (or referring to) a single element of an array, then the "dollar-sign" ($) sigil is used.

      For example:

      my @foo = qw/a b c/; print "$foo[1]\n"; # Prints b print "@foo\n"; # Prints a b c print "$foo[$#foo]\n"; # Prints c (but you would probably never do th +is) print "$foo[-1]\n"; # Also prints c (a better way than the previou +s) print "@foo[0,2]\n"; # Prints a c

      Darren :)

      For an array @array, $#array gives the index of the last item in the array. It's a minor piece of cryptic Perl syntax.

      G. Wade

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://728382]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2018-06-18 08:26 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.