Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Use hashes instead of grep

by space_monk (Chaplain)
on Nov 15, 2012 at 09:05 UTC ( #1003967=note: print w/ replies, xml ) Need Help??


in reply to How to grep exact string

Yes instead of using grep, why aren't you using hashes to determine whether there is anything new in the file? You can build up a hash of all the terms (machines) in file1 and then check the hash to see whether an entry exists for each line of file2.

This would be much quicker than using grep for what you seem to be doing.

A Monk aims to give answers to those who have none, and to learn from those who know more.


Comment on Use hashes instead of grep
Re: Use hashes instead of grep
by ColonelPanic (Friar) on Nov 15, 2012 at 10:22 UTC
    I agree that, all else being equal, this would be the best design.

    However, it ultimately depends on what the rest of the code does.



    When's the last time you used duct tape on a duct? --Larry Wall
      Not really. Even if the code uses both arrays then there is nothing preventing building a temporary hash through
      my %hash = map { $_ => 1 } @first_list;
      ..and then discarding the hash after the check for new machines. More likely is that he is only interested in the second list, or just new machines, and they could be in an array just like before.
      A Monk aims to give answers to those who have none, and to learn from those who know more.
        Yes, but this method is not clearly superior if arrays are needed for other purposes. The array comparison is simpler, uses less memory, and will be fast unless both files are huge.



        When's the last time you used duct tape on a duct? --Larry Wall

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-12-29 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (184 votes), past polls