Perl Hash if exists


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Hash if exists
# 1  
Old 02-01-2009
Perl Hash if exists

print $hash{$value} if exists $hash{$key};

would only print my top value, and not the one I want to "match".... should i not be using if exists? how would you recommend "searching" a hash file...

thanks!
# 2  
Old 02-01-2009
I assume you want to do something like this:

print $hash{$key} if exists $hash{$key};

If not, you need to elaborate more on what you are doing.
# 3  
Old 02-01-2009
My hash, for example, looks like this

One 1
Two 2
Three 3


The user is prompted to enter in a word:

my $input = <STDIN>;
chomp $input;

I first wanted to use this if equals, but not sure if I can implement, so I tried something like this:

my $blah;
for $blah (keys(%hash)) {
if ($input eq $hash{$blah}) {
print "$hash{$blah}";
}
else {
print "no";
}
}

Playing around with this, entering Three, I might get:
nono3

or if I use a last;
no

I guess just looking for an easy way to look through a hash for a key, and print the value. If you could point me in the right direction, it would be very helpful.
# 4  
Old 02-01-2009
Update, I made it through this by using:


if (exists $hash{$input}) {
print "This is equal to $hash{$input}\n";
}
else {
print "Not in hash\n";
}

So, if the user enters One, what prints is:
This is equal to 1

Now, I'm trying to go backwards, entering the value, and trying to get the key, so if the user enters 1, what prints is:
This is equal to One

Not sure how to go about this.... Learning, though!
# 5  
Old 02-01-2009
See if key is in a hash:

Code:
my $input = 'one';
chomp $input;
print exists $hash{$input} ? $hash{$input} : "$input is not a hash key";

Find a hash key given a value:

Code:
my $input = '1';
chomp $input;
foreach my $key (keys %hash) {
   if ($hash{$key} eq $input) {
      print "The key for $input is $key\n";
   }
}

# 6  
Old 02-02-2009
Thanks, unfortunately what's happening when I implement that script with an else statement that prints "no key found for value" is, if the value is the third in the hash, for example:

no key found for valueno key found for valueThe key for 1 is One

Any ideas? I want an "error message" for if the user enters something that's not a value....
# 7  
Old 02-02-2009
One way:

Code:
my $input = '1';
chomp $input;
my $flag = 0;
foreach my $key (keys %hash) {
   if ($hash{$key} eq $input) {
      print "The key for $input is $key\n";
      $flag++;
   }
}
unless ($flag) {
   print "There is no key for $input\n";
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

2. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

3. Shell Programming and Scripting

Help needed with if..exists in hash in perl

Hi, So I have a hash, %users = ("abc1" => "John Doe", "xyz2" => "Mary Jane"); and a variable my $who_user = `who am i|awk '{ print \$1F }'`; How do I use exists to check if the variable value is present in the hash if (exists($users{"$who_user"})) is not giving... (6 Replies)
Discussion started by: neil.k
6 Replies

4. Shell Programming and Scripting

Perl hash help

Hi , i have the below code its working fine when i execute in unix , but its not working in windows could you pls explain me where i am going wrong. This is the program $data = { '1' => 'one' , '2' => 'two' , 3 => 'three' }; print "hello : $data->{'1'}... (2 Replies)
Discussion started by: ragilla
2 Replies

5. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

6. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

7. Shell Programming and Scripting

perl hash

Hi i am reading one file and creating Hash from the contents of it my issue is there are 3 different files in 3 different locations having same structure so for parsing these files i have one subroutine which returns hash after reading all the 3 files i need to create consolidated hash from three... (2 Replies)
Discussion started by: zedex
2 Replies

8. Shell Programming and Scripting

perl using hash

i want to ask is it i can use hash in perl to store a page number with a list of words which is in that page and then print it out? Example Page 1 contains a are boy cat ............. (a list of sorted words) how can i store it in a hash? Thank you (3 Replies)
Discussion started by: mingming88
3 Replies

9. Shell Programming and Scripting

perl help on hash

I have line which is read from xml doc. I want to put this line into hash(perl variable). find line below and how i want to put this in hash <font size="10" type="int" name="ABC" > hash key should be size, type and name with corresponding value I doing as below:- $line =~ s/\s*.*?\s//;... (3 Replies)
Discussion started by: aju_kup
3 Replies

10. Shell Programming and Scripting

Perl Hash

hi i have two hash achi %disk1,%disk2 with( key, value) (key1,value1) How to store it in another hash.. Plz replyyy. Regards Hari (1 Reply)
Discussion started by: Harikrishna
1 Replies
Login or Register to Ask a Question