Hi Diego

Diego Aguirre wrote:
> Hello,
> I have just learned opening  and reading a file, with
>
> open (HoyIn,"File.txt");

Filehandles are traditionally all upper case, as:

    open (HOYIN, "File.txt");

> @rgstr=<HoyIn>;

This puts the <> operator into list context, so it will read
all of the file, placing separate records into consecutive
elements of the array. If this is what you want to do, then
you have finished with the file already and you can close
it here:

    close HOYIN;

> foreach $linea (@rgstr)
> {
>  print $linea ;
> }
> close (HoyIn);
>
> I want  read the file from -lets say- the fifth line on.  Any help,
> pls?

If you really want to use an array then you can iterate over a
'slice' of it, like this ($#rgstr is the index of the final element):

    foreach $linea (@rgstr[4..$#rgstr])

Also, if you miss out the control variable $linea, then Perl
defaults to using $_ instead, which is useful as many
operators also use it as their default parameter. So you
can write:

    foreach (@rgstr[4..$#rgstr]) {
        print;
    }

or just

    print foreach @rgstr[4..$#rgstr];

However, sucking all of the file into memory may be a
bad idea, especially if it is a large file. In this case you
can use a while loop on the filehandle:

    while (<HOYIN>) {
        print if 5..eof;
    }

Since you're using <> in a scalar context here it will read
individual lines into the $_ variable. The while condition
will succeed until you reach end-of-file, and the
conditional

    if 5..eof

will succeed on or after line number 5 until end-of-file.

HTH,

Rob




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to