in reply to code to get the first line of an array

A couple simple ways...

my $first_line = $array[0]; # or my $first_line = shift @array; # This is destructive to @array.

See perlintro and shift for details.


Dave

Replies are listed 'Best First'.
Re^2: code to get the first line of an array
by monarch (Priest) on Jul 12, 2005 at 06:56 UTC
    ..or
    my ( $first_line ) = @array;

    This works by the fact that the parenthesis denote a list, and you're copying the elements of the array into the elements in the list (of which there is just one in this example). This is non-destructive to the array.

    You can also view the second and third lines and soforth by continuing with this notion:

    my ( $first, $second, $third ) = @array;

    This is a technique I regularly use when parsing parameters passed to a function. The function parameters are placed in @_ and so I write:

    sub myfunction( $ $ $ ) { my ( $param1, $param2, $param3 ) = @_; # ... }