in reply to Columnwise parsing of a file
BrowserUk got in ahead of me. Here is one way to implement his solution:
#! perl use strict; use warnings; use Data::Dump; my @matrix; my $row = 0; push @{$matrix[$row++]}, split while <DATA>; dd @matrix; print 'element at col 3, row 2 is ', get_element(\@matrix, 3, 2), "\n" +; sub get_element { my ($matrix_ref, $col, $row) = @_; return $matrix_ref->[$row - 1][$col - 1]; } __DATA__ 20 30 40 60 70 80 90 100 49
Output:
23:50 >perl 548_SoPW.pl ([20, 30, 40], [60, 70, 80], [90, 100, 49]) element at col 3, row 2 is 80 23:50 >
Update: A simpler syntax for populating the array:
my @matrix; push @matrix, [ split ] while <DATA>;
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Columnwise parsing of a file
by ghosh123 (Monk) on Feb 26, 2013 at 09:51 UTC | |
by BrowserUk (Patriarch) on Feb 26, 2013 at 09:58 UTC | |
by Tux (Canon) on Feb 26, 2013 at 10:00 UTC | |
by tmharish (Friar) on Feb 26, 2013 at 09:56 UTC | |
by topher (Scribe) on Feb 26, 2013 at 16:15 UTC |