Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, wondered if anyone could help? I am just a beginner at perl and I am stuck.
while (<$file>) { print "$_\n"; }
How can i save the  $_\n into an array?? I dont wan to use  $line = $line . $_"\n"; because this doesn't print the file out in the same format. I need to keep all the whitespace from the original file and be able to print it all outside of the while loop. Thanks

Replies are listed 'Best First'.
Re: reading files
by Mr. Muskrat (Canon) on Dec 13, 2002 at 15:05 UTC

    Easiest:
    my @file = <$file>;

    Another option:

    my @file; while (<$file>) { push(@file, $_); }

      thanks but if i then print  @file it prints the array fine but removes all the whitespace I wanted to preserve! How can I get around this?

        Either the file you are reading doesn't contain the whitespace you think it does or you just can't see it when it prints. ;)

        Perhaps you are printing extra newlines?

        my @file = <DATA>; foreach (@file) { print $_,"."; } __DATA__ 2 spaces at the front and 2 spaces at the end one tab at the front and one tab at the end no spaces at the front or end but 5 spaces before the second 5

        Prints:
        2 spaces at the front and 2 spaces at the end . one tab at the front and one tab at the end .no spaces at the front or end but 5 spaces before the second 5 .

        When printing try:

        foreach $line (@file) { print $line."\n"; }

        Alternatively, you could change Mr. Muskrat's code as follows:

        my @file; while (<$file>) { push(@file, "$_\n"); }

Re: reading files
by gmpassos (Priest) on Dec 14, 2002 at 02:27 UTC
    Beginner?! Welcome!

    Here are some tips to read files:

    open (FILEHANDLE,"file.txt") ; @data = <FILEHANDLE> ; close (FILEHANDLE) ; chomp(@data); ## this will cut off the \n or \r ## in the end of each line.
    This will read all the file too, but is a better way for big files:
    open (FILEHANDLE,"file.txt") ; my $data ; 1 while( read(FILEHANDLE, $data , 1024*8 , length($data) ) ) ; close (FILEHANDLE) ;

    This will read line by line:

    open (FILEHANDLE,"file.txt") ; while ($line = <FILEHANDLE>) { ... } close (FILEHANDLE) ;

    If you are trying to open binary files, use binmode() after open:

    open (FILEHANDLE,"file.txt") ; binmode(FILEHANDLE) ;

    In your code you tell that don't want to do $line = $line . $_ ;
    This is not a good thing, because you rewrite all the variable for each loop, and is more slow. Do that:

    $line .= $_
    You will find the same thing for:
    $var += 10 ; # $var = $var + 10 ; $var -= 10 ; # $var = $var - 10 ; $var *= 10 ; # $var = $var * 10 ; $var /= 10 ; # $var = $var / 10 ;
    See the site http://www.perldoc.com for more!

    Graciliano M. P.
    "The creativity is the expression of the liberty".

Re: reading files
by Anonymous Monk on Dec 14, 2002 at 06:23 UTC
    # Ugly, I know, but it should work. The @temp array gets # overwritten each time through the loop with $_\n. Then, # it gets pushed onto the end of the @file array. while (<$file>) { print "$_\n"; $temp[0] = "$_\n"; push( @file, @temp); }

    update (broquaint): added <code> tags