in reply to reading multiple files one at a time

Your code will read only the first line from each file, since you do a "last;" after reading one line.

Get rid of the last and you'll read all the lines in each file. Or is it that you want to read the first line from each file, then the second, etc.?

  • Comment on Re: reading multiple files one at a time

Replies are listed 'Best First'.
Re^2: reading multiple files one at a time
by Anonymous Monk on Jun 11, 2005 at 02:12 UTC

    Hi,

    I know this is an old thread but this is what I am trying to do.

    I have three files and I want to merge them into one file a single line at a time.

    At the moment I have from the top post:

    @filenames = qw/English Kana Kanji/; foreach my $file (@filenames) { open FILE, "<$file"; while(chomp($line = <FILE>)) { print "$line "; last; } close FILE; }

    Output:

    Mr Tanaka たなかさん 田中さん

    However when I take out the 'last' I then get everything from my first file, then the second file then the third file.

    How do I do it so that I can merge the lines of each file. And then print the lines combined as in the above output?

    Many thanks,

    Peter.

      Updated: Corrected. Replaced the untested attempt to mitigate for warning produced when one or more of the input files is shoter than the others.

      #! perl -slw use strict; my @files = @ARGV; my @fhs; my $i = 0; open $fhs[ $i++ ], '<', $_ or die "$_ : $!" for @files; #while( my @lines = map{ scalar <$_> || () } @fhs ) { !!Bad code!! while( ## Build an array of one line (or the null string) from each fi +le. my @lines = map{ defined( my $line =<$_> ) ? $line : '' } @fhs ) { chomp @lines; ## Remove the newlines print join ' ', @lines; ## and concatenate them. } close $_ for @fhs; __END__ P:\test>465737 test1.dat test2.dat test3.dat file1 line 1 file2 line 1 file3 line 1 file1 line 2 file2 line 2 file3 line 2 file1 line 3 file2 line 3 file3 line 3 file1 line 4 file2 line 4 file3 line 4 ...

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
        Hi,

        I tried replying to this when you first posted it but couldn't. So I have just recently joined anyway cause this seems like a great Perl community.

        I just wanted to say thanks very much. It worked brilliantly.

        Is there any chance that you could explain what the following line does:
        while( my @lines = map{ scalar <$_> || () } @fhs ) {

        Specifically what are you doing in:
        scalar <$_> || ()

        Is this like forcing the $_ var to be of type scalar? Why do we want to do that? and what is the || ()

        If you or someone could explain this in easy newbie friendly terms that would be great.

        Thanks again.

        Peter.

      Unix systems already have a standard command line utility to do this. It's called "paste":
      paste file1 file2 file3 > merged_file
      And like all good unix tools, of course, it has been ported to windows (by cygwin, ATT Research Labs, GNU, and others).

      But using perl for this (as demonstrated by BrowserUK) is still fun and rewarding in its own right -- e.g. in case you need to do character encoding conversions while merging files, or manage column widths or modify column contents in intelligent ways. (You could use "paste" in combination with other standard unix tools to do these things, but at that point, it's not so different from just writing a little perl script.)