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

Re^3: using hashes

by kennethk (Monsignor)
on Sep 26, 2013 at 15:47 UTC ( #1055856=note: print w/ replies, xml ) Need Help??


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

This should work, and is clear to read. While it is not optimally efficient, efficiency shouldn't be your concern at this stage. If this isn't working, you need to post more information about your actual script. Posting real input, expected output, and actual code (all wrapped in <code> tags) will greatly facilitate the debugging. As discussed in How do I post a question effectively?.


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


Comment on Re^3: using hashes
Download Code
Re^4: using hashes
by R56 (Acolyte) on Sep 26, 2013 at 16:44 UTC

    It's only partially working, because it's not matching exactly the 'pattern-key'.

    Supposing I have both banana AND bananas, being two different names and banana having an ID of 25, he's translating like:

    25

    25s

    I'm guessing the [] exact match operator doesn't work on these cases?

      That is a trickier proposition. If you know your words are separated by word boundaries, you can solve your issue with the \b assertion:
      s/\b$find\b/$replace/g
      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.

        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 :)

        Is there a simple way to also contemplate names with hyphens on the middle?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-09-23 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (241 votes), past polls