in reply to get array number
So you want to lookup the field number given the field name? Hashes are usually great as lookup tables. You can use $. to determine whether this is the first line of the file or not (or you could create a flag).
use strict; use warnings; my %col_idx; open(my $result_fh, '<', 'file') # 3-arg safer or die("Unable to open input file: $!\n"); # $! meaningful while (<$result_fh>) { chomp(); # chomp safer my @fields = split(/;/, $_); if ($. == 1) { @col_idx{@fields} = 0..$#fields; } else { # Do what you want here. print "$fields[$col_idx{name}], "; print "$fields[$col_idx{postcode}]\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: get array number
by ikegami (Patriarch) on Jan 20, 2006 at 16:20 UTC | |
|
Re^2: get array number
by Fletch (Bishop) on Jan 20, 2006 at 14:16 UTC |