Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: using hashes

by BrowserUk (Pope)
on Sep 26, 2013 at 14:56 UTC ( #1055852=note: print w/replies, xml ) Need Help??


in reply to iterating hash keys?

and iterate through all the hash keys

Don't ever iterate hash keys! (Well, hardly ever :)

The major purpose of hashes is that you can lookup the value associated with any key directly, avoiding iteration.

For your purpose, the major part of the code should be something like:

while( <$names_to_be_replaced_file> ) { ## read each line s[\b([a-z]+)\b][ $name_id{ $1 } ]ge; ## find words, look them up + and replace them with the id print; ## Send the modified lines +to stdout }

Simple and very efficient.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

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

    Thanks for the help Browser, but apparently I'm way behind in Perl knowledge yet, as I don't really get the code... That's how I know I'm overcomplicating something that is really simple :|

    Is the $1 var pointing to the value of the hash?

      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

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Yes, it did!

        Your solution combined with Kenneth's apparently did the trick. Will try to work around it a little bit tho, see if I can make it quicker.

        Thanks for the help :)

      In order to make things even more complicated I recommend to replace $hash{ $1 } with

      $hash{ $1 } // $1

      which means if $1 is not found in your hash, then replace your word with itself, ie leave it unchanged.

          s[\b([a-z]+)\b][ $name_id{ $1 } ]ge;

      The 's' at the beginning says to find a pattern and replace it. The 'g' at the end says to repeat this process as many times as possible. The 'e' at the end says that the replacement part should be evaluated as code, not treated as literal text.

      In the first part, the pattern, the \b matches a "word boundary," the boundary between word characters and non-word characters like your commas. [a-z]+ means a string of 1 or more consecutive lowercase letters. The parentheses around that capture whatever is matched within them and save it in the special variable $1.

      In the replacement part, $1 contains the matched word, so this becomes a simple lookup for that word as a key in the %name_id hash, replacing it with the value corresponding to that key. As mentioned before, because of the 'g', this entire process is repeated for each match found in the line.

      Aaron B.
      Available for small or large Perl jobs; see my home node.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055852]
help
Chatterbox?
[Corion]: choroba: spod5 converts pod to S5 HTML, so it's also still basic. I find the lack of animations (in the sense of "highlight this", "highlight that" in code) somewhat tedious as I do it with rendered PNGs
[Corion]: I haven't found a good way to include/use the source SVGs I use for creating the PNGs directly as animations
[ambrus]: Presentations come in many different shapes, and so slides do as well.
[Corion]: Doing that in Powerpoint or Ooxml would be nice(r) but I'm much quicker doing the outline of a presentation and the code as Pod
[Corion]: Hurrr - on a machine that is behaving weirdly, I have two processes CROND running. I guess that is the source of unattended jobs sometimes not finding their files anymore...
[ambrus]: Most of the time if I make slides, they're just a formatted document with a medium level of formality (between a well written article and an informal draft), with usually the page breaks chosen carefully and possibly some content repeated between pages.
[Corion]: ambrus: My slides are mostly a list of things I want to talk about. But sometimes I want to point out interesting parts, or build up a larger image from small components. This is where animations/ highlights would come in well
[choroba]: what I like about slides in HTML + CSS is I can use templates to produce them, which means all code in slides is tested, and I can version them in git
[ambrus]: Btw, a request to everyone who organizes international conferences where presenters will show slides.
[ambrus]: In the document where you tell people how long the presentations are supposed to be, please also tell the aspect ratio of your projectors, because that's not obvious in advance.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2017-09-26 10:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (293 votes). Check out past polls.

    Notices?