in reply to Re^2: using hashes
in thread iterating hash keys?
Is the $1 var pointing to the value of the hash?
$1 captures the words in the string one at a time. This $hash{ $1 } looks that word up in the hash and returns the associates value (id). The ge causes the ids to be substituted for every word in the line.
Perhaps this will clarify things?
%hash = ( brown=>1, fox=>2, quick=>3, the=>4 );; $line = 'the quick brown fox';; $line =~ s[\b([a-z]+)\b][ $hash{ $1 } ]ge;; print $line;; 4 3 1 2
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: using hashes
by R56 (Sexton) on Sep 26, 2013 at 18:21 UTC |