Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

multicolumn files

by Anto_ch (Initiate)
on Jun 06, 2012 at 15:48 UTC ( #974756=perlquestion: print w/ replies, xml ) Need Help??
Anto_ch has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I am getting some troubles with arrays. I have two array with multiple columns and I'd like to merge them side by side. I tryed the join function but the output that gives is only the first array after the second one, instead to put the second next to first. Not that the arrays has not the same number of columns but have the same number of lines

here the sample array i use to test my code:

array 1

1 2 3 4 5

2 3 4 5 6

3 4 5 6 7

4 5 6 7 8

5 6 7 8 9

array2

a b c d

b c d e

f g h i

l m n o

p q r s

output I expect to have:

1 2 3 4 5 a b c d

2 3 4 5 6 b c d e

3 4 5 6 7 f g h i

4 5 6 7 8 l m n o

5 6 7 8 9 p q r s

output I have with join:

1 2 3 4 5

2 3 4 5 6

3 4 5 6 7

4 5 6 7 8

5 6 7 8 9

a b c d

b c d e

f g h i

l m n o

p q r s

use strict; use warnings; open (POSITION, "path to file"); my(@array1) = <POSITION> ; close POSITION ; open(APRI2, "path to file") ; my(@array2) = <APRI2>; close APRI2; my @join = (@array1, @array2); print @join; exit;
any help would be appreciated, thanks in advance A

Comment on multicolumn files
Download Code
Re: multicolumn files
by cheekuperl (Monk) on Jun 06, 2012 at 16:29 UTC
    #Lines read from Position $arr1[0]="1 2 3"; $arr1[1]="4 5 6"; #Lines read from APRI $arr2[0]="a b c"; $arr2[1]="d e f"; #Assume same number of lines has been read for($idx=0;$idx<=$#arr1;$idx++) { $comb=join(" ",$arr1[$idx],$arr2[$idx]); print "\nComb: $comb"; }
Re: multicolumn files
by stevieb (Hermit) on Jun 06, 2012 at 17:01 UTC
    my @array1 = ( "1 2 3 4 5", "2 3 4 5 6", "3 4 5 6 7" ); my @array2 = ( "a b c d", "b c d e", "f g h i" ); my @join; for my $elem ( @array1 ){ push @join, "$elem " . shift @array2; } print "$_\n" for @join;

    Output:

    1 2 3 4 5 a b c d 2 3 4 5 6 b c d e 3 4 5 6 7 f g h i

    Also, you should use the three-argument form of open(), and inform if something goes wrong:

    open my $fh, '<', 'path to file' or die "Can't open the damned file!: +$!";

      the array I used come from tab delimited text files, I tryied your code but I still get the same output as when I used join. thanks

        Ok, I tried it with files. Try changing the for loop I posted to this:

        for my $elem ( @array1 ){ my $arr2_elem = shift @array2; chomp( $elem, $arr2_elem ); push @join, "$elem $arr2_elem"; }
Re: multicolumn files
by 2teez (Priest) on Jun 06, 2012 at 18:06 UTC
    Hi,

    The code below could do what you wanted. I followed this logic because I see that the two files has the same number of rows.

    #!/usr/bin/perl use warnings; use strict; my $file1 = reader('file1.txt'); my $file2 = reader('file2.txt'); foreach my $col ( 0 .. $#{$file1} ) { print @{ $file1->[$col] }, @{ $file2->[$col] }, $/; } sub reader { my ($file) = @_; my @array; open my $fh, '<', $file or die "can't open file:$!"; while ( defined( my $line = <$fh> ) ) { chomp $line; next if $line =~ m/^$/; push @array, [ split /\s+/, $line ]; } close $fh or die "can't close file:$!"; return \@array; }

    output:
    12345abcd
    23456bcde
    34567fghi
    45678lmno
    56789pqrs

    Please see perldsc. Hope this helps.
Re: multicolumn files
by kcott (Abbot) on Jun 07, 2012 at 01:58 UTC
    "Not that the arrays has not the same number of columns but have the same number of lines"

    While this statement holds true for the number of lines, you can achieve what you want by changing

    my @join = (@array1, @array2); print @join;

    to

    print qq{$array1[$_] $array2[$_]\n} for (0 .. $#array1);

    I haven't tested this with your code. I did, however, check the technique on the commandline:

    $ perl -Mstrict -Mwarnings -E ' my @a1 = (q{1 2 3}, q{4 5 6}); my @a2 = (q{a b c}, q{d e f}); say qq{$a1[$_] $a2[$_]} for (0 .. $#a1); ' 1 2 3 a b c 4 5 6 d e f

    -- Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-09-23 19:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (240 votes), past polls