On Jul 12, jatuporn said:
>I try to write a program that reads a file with two fields.The first
>field is a costumer ID and the second is the costumer name by using " !
>" as a seperator between 2 fields. Store costumer ID as the key and the
>costumer name as value into a hash.
>
>My code is below, I have a problem that $info{ $key} does not show the
>value. What is the problem ? How to change it?
Because there's a newline at the end of the string.
>$filename ="costumer.txt";
>open(FILE,$filename ) or die ("can not open file : $filename");
>
>while ($line = <FILE>)
>{
You might want to chomp($line) here, to remove the trailing newline.
> $line =~ s/\s+/ /g;
> ($id,$name) = split(/!/,$line,2);
> $info{$id} = $name;
>}
>print "Enter id:";
>$key = <STDIN>;
You need to chomp($key) here.
>print ("ID: $key NAME : $info{ $key} \n");
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** Manning Publications, Co, is publishing my Perl Regex book **