in reply to line numbers and line content into hash

If you're trying to preserve the order in which lines were read out of a file, and maintain a one-to-one relationship with lines to array elements, I don't see what the problem is. The following snippet is intentionally overly explicit. It could be done in a couple of lines (or less), but I wanted to make it clear what the mechanics are.

my @array; while ( my $line = <DATA> ) { chomp; push @array, $line; } for( my $idx = 0; $idx <= $#array; $idx++ ) { my $line_no = $idx + 1; print "Line $line_no = $array[$idx]\n"; } __DATA__ Here's the first line Here's the second line Here is the third line Here is the forth line

Just always keep in mind that arrays are zero-indexed.

Hashes don't maintain order, and though it's possible to tie a hash to a class that keeps it sorted, most of the time you shouldn't be trying to enforce order on hashes. On the other hand, if you are using a hash for something where you need to see its keys in sorted order, just sort them.


Dave