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

Re: using hashes

by mtmcc (Hermit)
on Sep 26, 2013 at 14:51 UTC ( #1055850=note: print w/ replies, xml ) Need Help??


in reply to iterating hash keys?

What have you already tried? Also, have a look through this: How do I post a question effectively?


Comment on Re: using hashes
Re^2: using hashes
by R56 (Acolyte) on Sep 26, 2013 at 15:08 UTC

    Something like this: (assuming @lines as the array that has the input)

    for my $line (@lines) { while(my ($find, $replace) = each %ids) { s/$find/$replace/g } }
      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.

        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?

      For an effective solution to your problem, see BrowserUK's comment below. As to why the code you've shown doesn't work, it's probably because you're storing each line of your file/array in $line, but doing your substitution against $_. Try this: $line =~ s/$find/$replace/g.

        Thanks arrestee, that suggestion did some of the trick, but still having the 'exact match' problem that I replied to Kenneth on top...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-12-20 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (99 votes), past polls