Sun751 has asked for the wisdom of the Perl Monks concerning the following question:

To get rid of too many variables I used hash to record all my records in it,
sub initilize_config { my ($HR_config,$file) = @_; open (my $fr, '<', "$file") || die "Unable to open configuration f +ile: $file $!"; while (my $line = <$fr>) { $line =~ tr/\r\n//d; next unless $line; if ($line =~ /=/) { my ($key,$val)=$line=~ /^(.*?)=(.*?)$/; $$HR_config{$key} = $val; } } }
the key of the hash is constant where as value always change so in some subroutine if I want to extract and use the value of some key, suppose "xyz". How can that be done? Can any one suggest me please!!! Cheers

Replies are listed 'Best First'.
Re: Extracting values from hash
by moritz (Cardinal) on Jul 01, 2009 at 08:25 UTC
    See perldata and perlreftut, you can access the value of a hash reference like this: $HR_config->{xyz}.
Re: Extracting values from hash
by jrsimmon (Hermit) on Jul 01, 2009 at 11:39 UTC
    Here's a short little script that you can play with to see how this works.

    use strict; use warnings; my %HR_config= (); my $cfgFile = "c:\\temp\\cfg.ini"; &initiliaze_config(\%HR_config, $cfgFile); my (@retVals) = &retrieve_config(\%HR_config); print "Values from the returned array:\t@retVals\n"; my @keys = keys(%HR_config); print "Keys from the populated hash:\t@keys\n"; my @hashVals = map ($HR_config{$_}, @keys); print "Values from the populated hash:\t@hashVals\n"; sub initiliaze_config { my ($HR_config,$file) = @_; open (my $fr, '<', "$file") || die "Unable to open configuration f +ile: $file $!"; while (my $line = <$fr>) { $line =~ tr/\r\n//d; next unless $line; if ($line =~ /=/) { my ($key,$val)=$line=~ /^(.*?)=(.*?)$/; $$HR_config{$key} = $val; } } } sub retrieve_config { my $HR_config = pop(@_); my @values = (); foreach my $key (keys(%{$HR_config})){ my $value = $HR_config->{$key}; push(@values, $value); } return (@values); }