Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

joining files

by Anonymous Monk
on Oct 17, 2002 at 10:34 UTC ( #205980=perlquestion: print w/replies, xml ) Need Help??

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

Hello! I need to join two files in such a way that row number one in file_a is joined with row number one in file_b, and so on... I guess this is an easy one for wise Perlmonks... :) Thanx...

Replies are listed 'Best First'.
Re: joining files
by broquaint (Abbot) on Oct 17, 2002 at 10:58 UTC
    If you're happy to use modules
    use IO::File; use Functional qw( zip ); my $f1 = IO::File->new( shift ) or die("ack - $!"); my $f2 = IO::File->new( shift ) or die("ack - $!"); my $join = zip( [map { chomp; "$_ " } <$f1>], [<$f2>] );;
    Here's to hoping that more of Functional makes it into perl6.
    HTH

    _________
    broquaint

    update: added chomp() and extra space per dada's request

      ...and who's gonna chomp the lines? :-)

      regarding perl6, here's how I would do it (untested code, of course :-)

      require 6.0; # yeah, sure my $f1 is chomped = IO::File.new( shift ) or die("ack - $!"); my $f2 is chomped = IO::File.new( shift ) or die("ack - $!"); my @f1 is from <$f1>; my @f2 is from <$f2>; my @join = @f1 ^_ @f2;
      but this way you can't have a space between the lines from $f1 and $f2...

      Update: actually, this seems to work:

      my @join = @f1 ^_ " " ^_ @f2;
      ...I'm such a newbie :-)

      cheers,
      Aldo

      King of Laziness, Wizard of Impatience, Lord of Hubris

Re: joining files
by JaWi (Hermit) on Oct 17, 2002 at 11:19 UTC
    Beside all the Perl solutions given above, you could also use the join util, however, this only works if you've got GNU's textutils installed.
    This would make your code look something like this:
    ... my $joined_file = `join FILE1 FILE2` ...

    -- JaWi

    "A chicken is an egg's way of producing more eggs."

      JaWi is absolutely right. You should NEVER rebuild a wheel (especially a GREAT wheel that's already written in C by the GNU fellas) in perl unless you're on a system where you absolutely CANNOT use them or are writing something that HAS to be VERY portable.

      And in case you're wondering, you can even have the GNU utilities on a Win32 system. Check out http://gnuwin32.sourceforge.net/. It includes the join GNU utility. Excellent stuff, use it all the time.

      --
      perl: code of the samurai

Re: joining files
by Tanalis (Curate) on Oct 17, 2002 at 10:40 UTC
    Untested code, but ..

    while (my $file1 = <FILE1> and my $file2 = <FILE2>) { chomp $file1; chomp $file2; print join " ", $file1, $file2, "\n"; }

    --Foxcub

      That won't fly. If any of the files ends with a line containing just 0 and no newline, that will be skipped. Besides, why chomp both strings only to append a newline one line later?
      my $append; chomp, print "$_ $append" while defined($_ = <FILE1>) and defined($app +end = <FILE2>);
      Sidenote, I considered using defined($_ = <FILE1> and $append = <FILE2>) but that's broken: if $_ eq "0", it will false-shortcircuit $append and defined will test true - even though $append might be undefined.

      Makeshifts last the longest.

Re: joining files
by zigdon (Deacon) on Oct 17, 2002 at 14:18 UTC

    Note that most of these solutions (all of them?) will stop once one of the files runs out - if one file is longer than the other, the excess will be lost. Though, it is really trivial to append whatever's left after the join:

    # join files in whatever way you feel like if (not eof(F1)) { print <F1>; } elsif (not eof(F2)) { print <F2>; }

    -- Dan

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://205980]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2022-07-03 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?