4 Replies - 1822 Views - Last Post: 30 May 2010 - 07:02 AM

#1 tittup   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-May 10

How to use two hashes with different keys but same values in perl ?

Posted 27 May 2010 - 08:17 PM

i am a beginner in programming and i am trying to use two hashes with different states but same capital, here is my code


#!/usr/bin/perl -w

%states = ('Colorado' => Denver, 'Hawaii' => Honolulu);

%states2 = ('Co' => Denver, 'Hi' => Honolulu);

print "Enter the State :";
chop($find=<STDIN>);

if (exists($states{$find}))
{
print "\n$find Capital's is $states{$find}\n\n";
}
else
{
print "\n$find Capital's is $states2{$find}\n\n";
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to use two hashes with different keys but same values in perl ?

#2 dsherohman   User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: How to use two hashes with different keys but same values in perl ?

Posted 28 May 2010 - 04:38 AM

View Posttittup, on 28 May 2010 - 03:17 AM, said:

i am a beginner in programming and i am trying to use two hashes with different states but same capital

OK. You seem to have some code which should do that, aside from the quoting being incorrect in your hash definitions. The "fat comma" operator ( => ) automatically quotes what's on the left of it, not what's on the right, so they should be
my %states = (Colorado => 'Denver', Hawaii => 'Honolulu');
my %states2 = (Co => 'Denver', Hi => 'Honolulu');
or
my %states = ('Colorado' => 'Denver', 'Hawaii' => 'Honolulu');
my %states2 = ('Co' => 'Denver', 'Hi' => 'Honolulu');


I would recommend adding "use strict" at the top of your code. This will require you to declare your variables by putting "my" in front of them the first time they're mentioned (as I did above with your hash definitions), but, in return, you'll get protection against a wide range of common mistakes, including the quoting error in your hash definitions. If you had been using strict, your code would have failed to run with the error "Bareword "Denver" not allowed while "strict subs" in use".

Granted, that's not the clearest imaginable error message for the uninitiated, but at least it tells you where exactly the problem is.

If that doesn't answer your question, then you'll have to explain the problem more clearly, ideally including a description of both what your code is currently doing that you don't want it to and what it should be doing instead.
Was This Post Helpful? 0
  • +
  • -

#3 tittup   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 27-May 10

Re: How to use two hashes with different keys but same values in perl ?

Posted 28 May 2010 - 06:02 AM

thanks dave for the reply

To explain more,

using hash I want to enter an input, which is the state full name or an abbrev. then if the state"input" is in the hash then i will get the city.

it sounds simple but confusing for me !!
Was This Post Helpful? 0
  • +
  • -

#4 dsherohman   User is offline

  • Perl Parson
  • member icon

Reputation: 227
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: How to use two hashes with different keys but same values in perl ?

Posted 30 May 2010 - 06:15 AM

View Posttittup, on 28 May 2010 - 01:02 PM, said:

using hash I want to enter an input, which is the state full name or an abbrev. then if the state"input" is in the hash then i will get the city.

In that case, the simplest solution would be to use a single hash for your lookups. Hash keys are required to be unique, but the corresponding values can be duplicated as many times as you like, so this is completely legal:
my %states = (
  Colorado => 'Denver',
  Co       => 'Denver',
  Hawaii   => 'Honolulu',
  Hi       => 'Honolulu',
);


By using a single hash, you only have to do one lookup and either print the value found or an error message stating that the user's input wasn't recognized.

I'm happy to help with whatever other specific questions you have, but the overall picture isn't really something I can explain in much detail without just giving you a working program in the process. If that's what you want (and this isn't a homework assignment), I can do that, but I'm assuming that you want to learn this and work out the details for yourself rather than having the solution handed to you.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7506
  • View blog
  • Posts: 15,556
  • Joined: 16-October 07

Re: How to use two hashes with different keys but same values in perl ?

Posted 30 May 2010 - 07:02 AM

You're using a hash because you want it to do the searching for you. You're better off storing the data you want to store and then finding it how you need to find it. You could take your list of data and load that into a hash. However, it's simple enough just to loop.

Here's an example:
#!/usr/bin/perl -w

use strict;

my @states = (
	{ 'State' => 'Colorado', 'Abrev' => 'Co', 'Capital' => 'Denver' },
	{ 'State' => 'Hawaii', 'Abrev' => 'Hi', 'Capital' => 'Honolulu' },
);

sub getState {
	my($s) = @_;
	foreach my $item (@states) {
		if ($item->{State} eq $s || $item->{Abrev} eq $s) {
			return $item;
		}
	}
	
};

sub showCapital {
	my($s) = @_;
	my $item = getState($s);
	
	if ($item) {
		print $item->{State} . " Capital's is " . $item->{Capital} . "\n";
	} else {
		print $s . " not found.\n";
	}
}

showCapital('Hawaii');
showCapital('Hi');
showCapital('NY');


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1