You've got plenty of answers already, so I'll just play around a little:
$ cat u.pl my $data = "Region Item volume Month"; my %h = do {my $cnt=0; map {$_=>++$cnt} split /\s+/, $data}; print "$_ $h{$_}\n" for grep {exists $h{$_}} qw(Item volume Tuba); $ perl u.pl Item 2 volume 3
The hash maps the fields with the field index. Wrapping the right hand side of the hash assignment in a do statement lets me localize a temporary variable. Preincrementing the temp variable converts the field numbers to one-based numbering.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
In reply to Re: finding the field number of a matching pattern in perl
by roboticus
in thread finding the field number of a matching pattern in perl
by Thomas Kennll
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |