> -----Original Message-----
> From: James Ferree [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, August 08, 2001 4:42 PM
> To: James Ferree; [EMAIL PROTECTED]
> Subject: Re: Loading file into hash
>
>
>
> --- James Ferree <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > I am looking for a way to load a file containing the
> > following example data into a hash
> >
> > #From TO
> > Jim James
> > Art Arthur
> > John Arthur
> >
> > Then when $a=Jim I would like to convert it to
> > $a=James.
> >
> > Can this be done?
> >
> > Jim
> >
>
> Thanks to your help so far, I have the following:
>
> sub load_keys_file
> {
> open(KEY,$keys_file) || die "could not open $file:
> $!\n";
> while (<IN>)
This should be while(<KEY>), no? That's the filehandle you just
opened?
> {
> next unless (/\S/ && /^#/); # make sure
> there's something to input and line does not start
> with #
> ($old_phase,$new_phase,$ii_new_phase)=split /\t/;
> # split the line by tab
> $phase_hash{$old_phase}=$new_phase;
> $ii_hash{$old_phase}=$ii_new_phase;
> }
> close KEY;
>
>
> Now, how do I check to see if a hash value is defined?
>
> I thought this would work but I get a syntax error
>
> if ( $phase_hash($Phase))
You need curlies: $phase_hash{$Phase}
Now, the test
if ($phase_hash{$Phase})
is testing for the "truth" of the hash entry. It will be considered "false"
if it evaluates to "", "0", or numeric 0. All other values are considered
"true". Note that the undef value evaluates to "" when tested in this way.
There are actually three kinds of tests you can make:
if ($phase_hash{$Phase}) { ... } # tests for "truth"
if (defined $phase_hash{$Phase}) { ... } # tests for any value but undef
if (exists $phase_hash{$Phase}) { ... } # tests whether key exists
Read
perldoc -f defined
perldoc -f exists
for more details.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]