Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: finding the field number of a matching pattern in perl

by roboticus (Chancellor)
on Sep 16, 2012 at 14:16 UTC ( #993915=note: print w/ replies, xml ) Need Help??


in reply to finding the field number of a matching pattern in perl

Thomas Kennll:

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.


Comment on Re: finding the field number of a matching pattern in perl
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://993915]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (13)
As of 2015-07-28 20:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (258 votes), past polls