Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with hashes

Status
Not open for further replies.
Mar 11, 2004
127
GB
Me again...

I have a hash and I'm trying to access the keys using a specific value. Is it possible??

The simplest example I can give would be the following
Code:
#!/usr/bin/perl -w

%data =	(
	'Jan' => 31,
	'Feb' => 28,
	'Mar' => 31,
	'Apr' => 30);

$val = "31";

foreach $month (keys %data)
	{
	if ($val = $data{$val})
		{
		print "$month\n";
		}
	}

What I am trying to do is simple in my head, but maybe I'm not thinking "the perl way"? Or is there something I'm doing wrong?

Thanks in advance,
Anthony
 
$val is numeric, so you shouldn't put quotes around it.

You're also using the wrong operator to test for equality. '=' is the assignment operator, so you're replacing the value in $val with $data{$val} each time. For numeric comparisons, you want '=='. Finally you should use $data{$month} rather than $data{$val}, as it's the month that's the hash key.
 
Thanks for clearing those things up for me!

Hopefully this will give me the tools to go forward get over the hurdles I've got around this functionality.

Anthony
 
Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Date::Calc[/green] [red]qw([/red][purple]Days_in_Month This_Year Month_to_Text[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[olive][b]for[/b][/olive] [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$month[/blue] [red]([/red][fuchsia]1..12[/fuchsia][red])[/red] [red]{[/red]
	[olive][b]if[/b][/olive] [red]([/red][maroon]Days_in_Month[/maroon][red]([/red]This_Year, [blue]$month[/blue][red])[/red] == [fuchsia]31[/fuchsia][red])[/red] [red]{[/red]
		[black][b]my[/b][/black] [blue]$abbrev[/blue] = [url=http://perldoc.perl.org/functions/substr.html][black][b]substr[/b][/black][/url] [maroon]Month_to_Text[/maroon][red]([/red][blue]$month[/blue][red])[/red], [fuchsia]0[/fuchsia], [fuchsia]3[/fuchsia][red];[/red]
		[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$abbrev[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
	[red]}[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Other Modules used :
[ul]
[li]Date::Calc[/li]
[/ul]
[/tt]

- Miller
 
Thanks Miller. The example I was using above was the simplest version of what I was actually trying to do, but it was all sorted in the end. Good to know there are things like this though.

Ant
 
.. and in the spirit of TIMTOWTDI and redundancy:
Code:
print join "\n", grep { $data{$_} == $val } keys %data;

...which combines the print, the if and the foreach into one statement.

(I'm still learning, and love to play around with map and grep at the moment. Forgive me.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top