Here is another formulation for you using data from your OP. This makes a 2D array and then prints it. I tried to be straight-forward. Hope this is understandable to you. Pay attention to the code that prints each row of the 2D array. This type of thing occurs often.
#!/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
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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.