473,748 Members | 2,467 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 2266
KevinADC
4,059 Recognized Expert Specialist
I believe you about the unique key part.

Ii'm trying to figure out how I'm gonna bypass that problem. probably by working with arrays, which is easy, instead of hashes, but that will be slow with big files..

Do you know any way of creating a hash system in which I can store the names: like "Harry Potter..." and the productcode of the object. With the knowledge that there will be items with identical names but different productcodes AND that I need to be able to find the productcodes of all items mathing a specified name? (because I need to implement a "search" option based on product names)
Don't be offended, but judging by the code you posted you need to master some basics first. The way you are reading the data in from the file is a bit convoluted and slow, and the way you are building the hash is not correct.

Generic example:

Expand|Select|Wrap|Line Numbers
  1. my %hash;
  2. open(FH,'file') or die "$!";
  3. while(<FH>) {
  4.    chomp;
  5.    my @data = split(/\|/);
  6.    push @{$hash{$data[1]}},$data[0];
  7. }
  8. close FH;
  9.  
That will create a hash of arrays from the first two data fields of each line. To access the array that is associated with the hash key (assumes you know the name of the hash key):

Expand|Select|Wrap|Line Numbers
  1. foreach my $id (@{$hash{'Harry Potter'}}) {
  2.      print "$id\n";
  3. }
  4.  
which is really the same as a regular array with the exception of some extra symbols @{} to dereference the array that the hash key points to.
Feb 27 '08 #11
eWish
971 Recognized Expert Contributor
If you plan on having your product line growing quickly, then I would suggest that you use a database to handle your data for you. It would simplify things for you greatly.

--Kevin
Feb 27 '08 #12
minowicz
12 New Member
Actually, what you are trying to do is not so ill advised as some have tried to make out. It sounds like what you really want to do is search through your database for products that match a given title. A hash is a great way to search for things, but unfortunately, as has been mentioned, the hash keys need to be unique.

The first possible work-around for this would be to simply use the product ID as your key. This gets you something unique, but means that to 'search' you'll have to iterate through all the keys and check for a value that matches the title for which you are searching.

My suspicion is that you want to avoid such a search by instead creating an index with a hash. The problem of course is that you can't simply use the title as the key to your has and associate the product ID with it since each new occurrence of the same title will result in overwriting the previously associated value.

Instead though, what you could do is create what is called a hash of arrays.

Rather than simply storing the value in the hash under a certain key, you instead could have each key associated with a reference to an array. Instead of putting the product ID into the value for a given key, you would add it to the array associated with the key. This gives you a hash that looks like this:

Expand|Select|Wrap|Line Numbers
  1. my %search_index = (
  2.   'Title One'  => ['book-123', 'dvd-456'],
  3. );
  4.  
  5.  
Then you'll simply be able to look for the key in the has and find out that book-123 and dvd-456 both match that title. A further lookup into the has that uses the product IDs as its keys will give you the full details for either or both of these matches. This scales out fairly well.

I'd post some example code, but I'm in a bit of a rush. Still, there are plenty of good guides and chapters in books on hashes of arrays and other data structures. One good place to start is

Expand|Select|Wrap|Line Numbers
  1. perldoc perldsc
  2.  
Feb 27 '08 #13
KevinADC
4,059 Recognized Expert Specialist
Instead though, what you could do is create what is called a hash of arrays.
While you explained it in more detail, that is the same suggestion I already made in a previous post. There has been no mention of what he is trying to do to be ill advised that I noticed, it's the way his going about doing it that needs work.

Not that there is anything wrong with repeating what others have already suggested, especially since you took the time to elaborate on it more.

Hope to see you around here more often.

Regards,
Kevin
Feb 27 '08 #14
Gangreen
98 New Member
Thanks for the help and explanation, I appreciate it.
I'm gonna try the hash of arrays

thanks
Feb 27 '08 #15
minowicz
12 New Member
There has been no mention of what he is trying to do to be ill advised that I noticed
Perhaps that was a poor choice of words on my part. Still, I took the following:

If you plan on having your product line growing quickly, then I would suggest that you use a database to handle your data for you. It would simplify things for you greatly.
To mean that how the original poster intended to solve the problem was somehow deficient and far better served by using a database rather than a hash internal to the perl program. This may well be true, depending on the current and expected sizes of the database and the resources available on the machine that will be running the code.

It is unlikely that the use of a database would actually simplify things for the OP though. Instead it is likely to complicate things, at least in the short term. Selecting a database to use, learning SQL, and introducing a dependency on additional software that may not already be available on the machine where the script will run...

We already know that the current data is kept in a flat file. At a minimum, a hash of arrays or a hash of hashes is quite likely to be an improvement on that.
Feb 27 '08 #16
Gangreen
98 New Member
MySQL is no problem, I'm pretty good with it, but i'm simply not allowed to use that in my assignment.
Feb 27 '08 #17
Gangreen
98 New Member
You certainly helped me. I got it working now.

I have a new problem, but I'm not gonna sart a new topic, and instead I'll just post it here since it's about searching in hashes as well.

One question: how can i search for keys in a hash using a regular expression?
because if I do something like this:

if (exists $inventoryCodes {qr/.*/})

It doesn't find anything...
Feb 28 '08 #18
KevinADC
4,059 Recognized Expert Specialist
You certainly helped me. I got it working now.

I have a new problem, but I'm not gonna sart a new topic, and instead I'll just post it here since it's about searching in hashes as well.

One question: how can i search for keys in a hash using a regular expression?
because if I do something like this:

if (exists $inventoryCodes {qr/.*/})

It doesn't find anything...

You have to loop through a hash and check each key to find something with a regexp:

Expand|Select|Wrap|Line Numbers
  1. foreach my $key (keys %hash) {
  2.    if ($key =~ /foo/) {
  3.        found it
  4.    }
  5. }
  6.  
You can also use grep on the hash keys but it might not be as efficient unless you wanted to find hash keys that are similar in some way.

Keep in mind that hash keys have to be simple strings, so what you posted just looks to see if there is hash key in the hash table that is literally 'qr/.*/' it does not try and match a hash key using a compiled regexp if thats what you are hoping.

But are you really searching for a hash key or a hash value? If it's a value and you don't know the name of the key you have to loop through the hash and use a regexp to search each value:

Expand|Select|Wrap|Line Numbers
  1. foreach my $key (keys %hash) {
  2.    if ($hash{$key} =~ /foo/) {
  3.        found it
  4.    }
  5. }
  6.  
Or use grep to find multiple matches.
Feb 28 '08 #19
Gangreen
98 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.
Feb 28 '08 #20

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
1506
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
1326
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
9555
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...
1
9329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9250
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
4878
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.