Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

read column in perl

by dee00zee (Novice)
on Mar 08, 2005 at 07:08 UTC ( [id://437434]=perlquestion: print w/replies, xml ) Need Help??

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

I have 2 files to read from, namely file1 and file2.
I want to insert the content of file1 to be the
2nd column of file2.

content of file1

a b c d e

content of file2

1 6 11 2 7 12 3 7 13 4 8 14 5 9 23

I want the output to be as follows

1 a 6 11 2 b 7 12 3 c 7 13 4 d 8 14 5 e 9 23

My code didn't return the result I want

#!/bin/perl open(IN,"file1"); open(IN2,"file2"); open (OUT,"> output"); while($str=<IN2>) { @array=split(/\s+/,$str); while($data=<IN>) { @box=split(/\s+/,$data); foreach $item(@array) { splice (@box,2,0,"$array($item)"); print OUT "@box\n"; } } }

20050308 Janitored by Corion: Added code tags.

Replies are listed 'Best First'.
Re: read column in perl
by jmcnamara (Monsignor) on Mar 08, 2005 at 09:09 UTC

    Here is a one-liner for a Unix system:
    $ paste file1 file2 | perl -lane 'print "@F[1,0,2,3]"' 1 a 6 11 2 b 7 12 3 c 7 13 4 d 8 14 5 e 9 23
    Or this:
    $ paste file1 file2 | awk '{print $2, $1, $3, $4}'

    You can change the field separator in either example to suit.

    --
    John.

      I wish I could ++ that 5 times. It showed me a new UNIX command, and the perl arguements at the same time. Great work!

      ----
      My mission: To boldy split infinitives that have never been split before!

        I like to remind people that Unix isn't there just to bootstrap perl. ;-)

        --
        John.

Re: read column in perl
by ikegami (Patriarch) on Mar 08, 2005 at 07:20 UTC

    Only one loop is needed.

    #!/bin/perl open(IN, "file1"); open(IN2, "file2"); open(OUT, "> output"); while ($str = <IN>) { chomp($str); $data = <IN2>; chomp($data); @box = split(/\s+/, $data); splice(@box, 1, 0, $str); print OUT (join(' ', @box) . "\n"); }
Re: read column in perl
by jbrugger (Parson) on Mar 08, 2005 at 07:20 UTC
    Hi dee00zee, I see you've written a few nodes now, and i wonder why you're not using <code> tags.
    Since you're not totally new here, i assume you have read Perl Monks Approved HTML tags and Writeup Formatting Tips?
    #!/bin/perl use strict; open(IN,"file1"); open(IN2,"file2"); open (OUT,"> output"); while (my $str = <IN>) { chomp($str); my $data = <IN2>; $data =~ s/(.*?)\s(.*)/$1 $str $2/; print OUT ($data); }
Re: read column in perl
by sh1tn (Priest) on Mar 08, 2005 at 08:02 UTC
    use strict; open IN1, "file1" or die $!; open IN2, "file2" or die $!; open OUT, ">output"or die $!; my @file1; my @file2; chomp, push @file1, $_ while <IN1>; chomp, push @file2, $_ while <IN2>; close IN1; close IN2; my $i = 0;$file1[$i] .= "\t".(join "\t", split '\s', $_), $i++ for @fi +le2 ; print "$_$/" for @file1; print OUT "$_\n" for @file1; __END__ a 1 6 11 b 2 7 12 c 3 7 13 d 4 8 14 e 5 9 23


      You don't want to read the whole file in if you don't have to - that doesn't scale well to files that are larger than your physical RAM. (Swap space is so slooooow!)

      Furthermore, the following can be rewritten:

      my @file1; my @file2; chomp, push @file1, $_ while <IN1>; chomp, push @file2, $_ while <IN2>; ############## chomp(my @file1 = <IN1>); chomp(my @file2 = <IN2>);

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-26 04:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found