in reply to array in different columns
Update: I thought I'd comment on this: split (' ',$col). This does not mean "split on the space character". This is a special case coded into Perl. This means "split on any sequence of white space characters". There are five: space,line feed,carriage return,tab,form feed. There is a difference in how leading white space is handled between that split and split (/\s+/,$col) which doesn't matter here and gets us a bit far afield.#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @array; $array[0]="0\n1\n2\n3\n"; $array[1]="4\n5\n6\n"; $array[2]="7\n8\n"; $array[3]="9\n"; my @twod_array; my $i_col=0; foreach my $col (@array) { my $i_row = $i_col; #we go down a diagonal to right my @row_stuff = split (' ',$col); foreach my $ele (@row_stuff) { $twod_array[$i_row++][$i_col] = $ele; } $i_col++; } foreach my $row_ref (@twod_array) { my $line = join ("\t", @$row_ref); print "$line\n"; } =prints: 0 1 4 2 5 7 3 6 8 9 =cut
|
|---|