Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re^5: using hashes

by kennethk (Abbot)
on Sep 26, 2013 at 17:44 UTC ( #1055871=note: print w/replies, xml ) Need Help??

in reply to Re^4: using hashes
in thread iterating hash keys?

That is a trickier proposition. If you know your words are separated by word boundaries, you can solve your issue with the \b assertion:
That will require that either side of your key must correspond to the start or finish of the line, or to the pattern \w\W or \W\w.

If you can't use word boundaries to delimit, you could try your keys in descending key length.

for my $line (@lines) { for my $find(sort {length($b) <=> length($a)} keys %ids) { $line =~ s/$find/$ids{$find}/g; } }

This still has some potential for key overlap, but we'd really need to see you real data to design the correct regex for it. There are a number of ways to cut this up, the choosing the 'right' answer depends strongly on actual input.

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Replies are listed 'Best First'.
Re^6: using hashes
by R56 (Sexton) on Sep 26, 2013 at 18:16 UTC

    The \b assertion apparently did the trick.

    It will take at least a couple of hours until all is processed (as soon as I get all the basics, I'll start working on my optimization :), but by the looks of it, it looks okay!

    Thanks for all the help and patience Kenneth :)

Re^6: using hashes
by R56 (Sexton) on Sep 27, 2013 at 13:26 UTC
    Is there a simple way to also contemplate names with hyphens on the middle?
      Hyphens aren't special outside of a character class. What makes you think they are at fault?

      If your 'words' might contain punctuation, you can escape meta characters using quotemeta or synonymously \Q .. \E


      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        Imagine these values:

        banana => 25

        bana => 20

        bana-na => 15

        na => 10

        For the translation to, let's say:


        Currently the output is:


        That's what made me thought the hyphens were a special case...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055871]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (14)
As of 2018-05-21 13:21 GMT
Find Nodes?
    Voting Booth?