hash problem

M

Monkey Man

somebody help me with this

#!/usr/bin/perl -w

%name = ( "ziya" => "aktas", "kimberly" => "aktas", "azer" => "apaydin" );

print "Enter Something: ";
my ($key) = <STDIN>;
print $name{ $key };



it just doesn't work
 
B

Brian Wakem

Monkey Man said:
somebody help me with this

#!/usr/bin/perl -w

%name = ( "ziya" => "aktas", "kimberly" => "aktas", "azer" => "apaydin" );

print "Enter Something: ";
my ($key) = <STDIN>;
print $name{ $key };



it just doesn't work


$key has a \n on the end. You need to chomp $key before printing.

chomp $key;
 
G

Gunnar Hjalmarsson

Monkey said:
somebody help me with this

#!/usr/bin/perl -w

%name = ( "ziya" => "aktas", "kimberly" => "aktas", "azer" => "apaydin" );

print "Enter Something: ";
my ($key) = <STDIN>;
-----^----^

Why did you add those parentheses, compared to the (almost) identical
question you asked 16 minutes before you posted this "hash problem"
question? Anyway, you'd better study this section in the FAQ:

http://www.perldoc.com/perl5.8.0/pod/perlfaq7.html#Why-doesn't--my($foo)-=-<FILE>---work-right-
print $name{ $key };

Just combine the FAQ answer with the answers posted by Ben and Brian.
 
W

Web Surfer

[This followup was posted to comp.lang.perl.misc]

somebody help me with this

#!/usr/bin/perl -w

%name = ( "ziya" => "aktas", "kimberly" => "aktas", "azer" => "apaydin" );

print "Enter Something: ";
my ($key) = <STDIN>;

chomp $key; # remove the trailing newline character
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top