in reply to How to read each line of a file into a nested array
Unless the data is sequential and non-sparse a hash would be better suited for this than an array.
The following is a simple example to get you started:
#!/usr/bin/perl -w use strict; my %hash; while (<DATA>) { chomp; next unless /\S/; # Skip blank lines /(\d+)/; # Match the first number $hash{$1} = $_; } for my $key (sort {$a <=> $b} keys %hash) { print $key, "\t", $hash{$key}, "\n"; } # Prints: # 1 1 2 3 4 5 # 10 10 20 30 40 50 # 200 200 100 300 2 1 __DATA__ 1 2 3 4 5 10 20 30 40 50 200 100 300 2 1
You might also wish to split the data on each line and store it as an array ref in the hash. Have a look at the perldsc manpage for more information.
--
John.
|
|---|