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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

eval a hash variable inside function 1

Status
Not open for further replies.

J1mbo

Programmer
Dec 12, 2002
93
US
I have this code that parses smb.conf and puts a users shares/descriptions into a hash of the username:

open(CONFIG, "$smbconf") || die;

while (<CONFIG>) {
next unless m/^\[([a-zA-Z0-9]+)\]$/;
$ShareName = $1;
while (<CONFIG>) {
next unless m/^\s+comment \= (.*)$/;
$ShareDesc = $1;
while (<CONFIG>) {
next unless m/^\s+valid users \= (.*)$/;
@ValidUsers = split(",", $1);
foreach $USER(@ValidUsers){
$$USER{$ShareName} = "$ShareDesc";
}
last;
}
last;
}
}
close(CONFIG);


I'm having problems passing a username into a function to pull out the stored hash values:

sub get_user_shares {
$username=@_;
while( ($key, $value) = each(%$username) ) {
printf "%-12s %30s\n", $key, $value;
}
}

$USERNAME = "smbuser01";
&get_user_shares("$USERNAME");



'%$username' does not eval to '%smbuser01'. I've tried using eval w/o success. Maybe i'm missing something...

Any ideas?

Thanks in advance,

jim



 
Thanks, but it has to be a variable going into the function since the function will be processing more than just the one user in my example.

 
In any case
my %hash=....; #(your hash)

Now if you want the function to take one key $key and return its value:

my $value= get_hash_value(\%hash,$key);

sub get_hash_value{
my $rhash=shift; #hash reference
my $key=shift;
die "not a hash key $key\n" unless exists $rhash->{$key};
return $rhash->{$key};}


If you want the function to print out all key-value pairs, then
get_hash_values(\%hash );

sub get_hash_values{
my $rhash=shift; #hash reference
forech my $key(sort keys %{$rhash}){
print "$key $rhash->{$key}\n";}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top