473,748 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Searching in Hashes

98 New Member
There is a hash in my program.
The problem is I need to search in the hash, but there are some keys that are identical, but I need to get the associated value of all these keys.

when I do something like

$value = $hash{ "theKey" };

obviously I'm only getting the value associated to the first key called "theKey". How can I get the others? Some sort of loop?

thanks
Feb 25 '08
22 2263
minowicz
12 New Member
it's the keys I'm comparing. It's to expand the search I made with the help of this topic by adding functionality like searching for:

'keyword 1' AND 'keyword2'

so both keywords need to be in the string representing the key.

also an 'keyword 1' OR 'keyword2'

and also searching for one word in keys (given the fact the keys are sentences: i.e "Harry Potter and ..." )

I was just hoping there would be a more efficient way than just check each value and compare...becau se that won't give hashes an advantage in speed against arrays.

OK, so to some extent, you really want to be able to query all sorts of possible matching situations against the data. Eventually, yes, you are going to end up wanting an SQL-like syntax to express searches. So you really need to stop and ask yourself how much farther your search semantics are going to evolve and if you're going to pass a point where the code your writing would have been better served with an SQL database. For simple searches, you just don't need one, but the more complex and varried your searches are going to become, the more of a payoff you're going to get from the investment in going to the effort.

That said, I don't think that what you're trying to do so far has quite reached that point.

At this point though I think you're best off keeping your original data in a hash with the product ID as the key and building arrays that index into that hash for the various searches you expect to be most common.

Assuming your inventory is kept in a hash that looks something like this:

Expand|Select|Wrap|Line Numbers
  1. my %inventoryDB = (
  2.     DVD-432 => ['Harry Potter and the Prisoner of Azkaban'. '25.00', '5'],
  3.     BOOK-432 => ['Harry Potter and the Prisoner of Azkaban', '15.00' '7'],
  4. );
  5.  
You'd build a title index that looked something like this:

Expand|Select|Wrap|Line Numbers
  1. my %titleIDX = (
  2.     'Harry Potter and the Prisoner of Azkaban' => ['DVD-432', 'BOOK-432'],
  3. );
  4.  
And you could even go so far as to build a keywords index by iterating through titles and other such things to create indexes like:

Expand|Select|Wrap|Line Numbers
  1. my %keywordIDX = (
  2.     'harry' => ['DVD-432', 'BOOK-432'],
  3.     'potter' => ['DVD-432', 'BOOK-432'],
  4.     'prisoner' => ['DVD-432', 'BOOK-432'],
  5.     'azkaban' => ['DVD-432', 'BOOK-432'],
  6. );
  7.  
Though such an index would get rather large and do so quickly. (You'd end up doing better to start using something like Lucene at some point going down this path.)

Constructs such as these allow you to do things like grep or pattern match across the keys of the hashes. Depending on how you intend to update the information in the database, you could even build arrays of the keys ahead of time just to avoid repetitive calls to keys().

Expand|Select|Wrap|Line Numbers
  1. my @titleKEY = keys %titleIDX;
  2.  
You'd then need to regenerate indexes and arrays with lists of keys either on every update that would change them, or on a periodic basis, understanding that they'd be out of date until such an update occurred. (This is largely a function of how 'live' the data is supposed to be.)

Very quickly this would lend itself to a bit of object orientation in which you'd presumably have objects for both individual inventory items and for the inventory database itself. Then the various instance data accessors would would have code that would understand when an update would require a re-index and some part of the indexes, potentially triggering a cascade of re-indexes along the way.

The upshot is that this all gets very complex in an effort to save you from simply doing:

Expand|Select|Wrap|Line Numbers
  1. foreach my $key (keys %hash) {
  2.     if ($hash{$key} =~ /foo/) {
  3.         found it
  4.     }
  5. }
  6.  
I guess what I'm saying is that this last little code snippet may not be ideal, but it is always up to date with whatever is in your hash, and it is really simple to code. You don't need to worry about complex re-indexing schemes and so on.

If you really have a need for making this simple to do, simple to extend, AND high performance (so much so that you're looking att using multiple arrays with common numerical indexes rather than hashes for performance reasons) then I'm finally willing to admit that you should go ahead and start using an SQL database now.
Mar 3 '08 #21
eWish
971 Recognized Expert Contributor
minowicz,

What would be the main reason you would suggest the OP move towards using a database? Also, from the standpoint of performance, what gain(s) does the database have to offer vs the use of multiple hashes and/or arrays?

--Kevin
Mar 4 '08 #22
minowicz
12 New Member
minowicz,

What would be the main reason you would suggest the OP move towards using a database? Also, from the standpoint of performance, what gain(s) does the database have to offer vs the use of multiple hashes and/or arrays?

--Kevin
Actually, I'm largely opposed to the idea of moving this to a database unless one of two conditions are true:
  1. The total size of the database is large enough that it will not readily fit into memory when all of the various indexes and such are considered along with the original data.
  2. The search semantics required are so elaborate, varied, and/or changing through the application life cycle that the OP is likely to have to reinvent the wheel of SQL.

There are probably solutions for either of these situations that would not require a move to an SQL database, but the original question was a fairly simple case. New requirements in the follow-on seem to be revealing the likelihood of #2 above.

One simple possibility, particularly given the original data format would be DBD::RAM. More speed and/or a smaller memory footprint could probably be had from DBD::DBM or possibly DBD::SQLite or DBD::SQLite2 all listed on CPAN. Moving to something like MySQL or PostgreSQL are other possibilities if the situation warrants it.

Again, my contention is that this all *could* be done in Perl, but if there are going to be more complex searches, changing search needs, the need to keep the data 'live; for both searches and updates, complex rules for updating only the indexes that need to be updated for a given change, and so on... Well, there comes apoint when it is wise to be lazy and rely on systems that others have already built that do such things and do them very well.
Mar 4 '08 #23

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1545
by: Carl S. in 't Veld | last post by:
I am trying to generate tiger tree hashes the same way as directconnect does, but I am failing. I checked the output from php with the reference vectors from http://www.cs.technion.ac.il/~biham/Reports/Tiger/test-vectors-nessie-format.dat and they appear to be different! echo bin2hex(mhash(MHASH_TIGER, 'abc'))."<br/>\n"; outputs f258c1e88414ab2a527ab541ffc5b8bf935f7b951c132951
2
1505
by: Michael McGarry | last post by:
Hi, Are there hashes in Python? Michael
8
2748
by: Ben Holness | last post by:
Hi All, I want to create a hash array, based on values in a database. Basically, I want a hash array for each database key and I want to use a sub to get the hash array, but I am having a great deal of difficulty!! I have written an example script, taking out the DB side of things, to explain what I want to do and how I want to do it. I am obviously doing something wrong, but I don't know what :)
2
4021
by: Jeff Thies | last post by:
I need to check if two hashes are identical. My thoughts are something like this: function compareHash(hash1,hash2){ if(hash1.length != hash2.length){return false} for(var key in hash1){ if(hash1 != hash2) return false;
6
5494
by: Jonathan | last post by:
I am hoping that someone more experienced than myself can point me towards what might be the fastest data lookup method to use for storing ip addresses. My situation is that I will need to maintain a list of perhaps 50 ip addresses to be used in a packet sniffing application. For each packet that goes through the application (which will be monitoring all traffic through a switch), I need to see if an entry for the source ip of that packet...
2
2855
by: MartyNg | last post by:
I am running a system that has both Classic ASP applications and a smattering of ASP.NET applications. We want to store passwords on a SQL Server table as their MD5 hashes. What is the safest way to get this hash value, and be able to verify it against user logins in both Classic ASP and ASP.NET? We have been working with the system.security.cryptopgraphy MD5 functions in .NET, and the functions here for Classic ASP...
0
1325
by: HalfCoded | last post by:
hi everyone, I am kind of stuck and therefore would really appreciate some clues: I actually have to run a script which has to compare two elements from two different files which are a blast file and a cdf file I need also to keep the data structure For this I chose the following strategy: -dumping the files into two arrays -doing a pattern matching between the two files.
1
3849
by: pavanponnapalli | last post by:
hi , I have come across a code as below in the perldoc: %hash = map { get_a_key_for($_) => $_ } @array; Could anybody tell me the meaning of the above line? please explain map function with reference to hashes. I have understood the meaning of map in general and have worked with an example. But i could not understand the meaning of the same with respect to hashes. So please help me. Thanks & Regards, pavan.
10
6556
by: aurekha | last post by:
Hi I have hashes with arrays to its keys like, %h1 = ('a'=>, 'b'=>, 'c'=> ); %h2 = ('a'=>, 'b'=>); then, how can i compare the 2 hashes(based on values. not keys) and get distinct values ?
6
9610
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function serialization The code itself generates a list of strings comprised of random numbers. No number will be repeated within a string, and no string will be repeated within the list of strings. Following the code is a brief discussion of how the above...
0
9552
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9249
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.