Perl: Return hash from subroutine
arrays, hash, perl, subroutine
Solution
First off, as mpapec mentioned in comments, `use strict; use warnings;`. That will catch most common mistakes, including flagging most of the problems you're asking about here (and usually providing hints about what you should do instead).
Now to answer questions 1 and 2:
`%hash` is the hash as a whole. The complete data structure.
`$hash{key}` is a single element within the hash.
Therefore, `\%hash` is a reference to `%hash`, i.e., the whole hash, which appears to be what you intend to return in this case. `\$hash{key}` is a reference to a single element.
Where it gets tricky in your second question is that references are always scalars, regardless of what they refer to.
`$hash_ref = \%hash`
To get an element out of a hash that you have a reference to, you need to dereference it first. This is usually done with the `->` operator, like so:
`$hash_ref->{key}`
Note that you use `->` when you start from a reference (`$hash_ref->{key}`), but not when you start from an actual hash (`$hash{key}`).
(As a side note on question 2, don't prefix sub calls with `&` - just use `authUser()` instead of `&authUser()`. The `&` is no longer needed in Perl 5+ and has side-effects that you usually don't want, so you shouldn't get in the habit of using it where it's not needed.)
For question 3, if you're only going to check once, you may as well just loop over the array and check each element:
my $valid;
for my $username (@list_of_users) {
if ($login eq $username) {
$valid = 1;
last; # end the loop since we found what we're looking for
}
}
if ($valid) {
print "Found valid username $login\n";
} else {
print "Invalid user! $login does not exist!\n";
}
Problem
I have been trying examples for hours but I can't seem to grasp how to do what I want to do. I want to return a hash from a subroutine, and I figured a reference was the best option. Here's where it gets a bit tricky. I want to reference a hash like $hash{$x}. I am still a noob at perl :/ 1.First question, the examples I use seem to show it is ok to use $hashTable{$login}, should I be using %hashTable{$login} or does it not matter? Below is the code: ``` sub authUser { $LocalPath = "/root/UserData"; open(DATAFILE, "< $LocalPath"); while( $linebuf = <DATAFILE> ) { chomp($linebuf); my @arr = split(/:/, $linebuf); my $login = $arr[1]; # arr[1] contains the user login names my $hashTable{ $login } = "$arr[0]"; #$arr[0] is account number } close DATAFILE; return \$hashTable{ $login }; } ``` I then want to test this data to see if a login is present, here is my test method ``` # test login Dr. Brule which is present in UserData my $test = "Dr. Brule"; my $authHash = &authUser(); if ( $authHash{ $test } ) { print "Match for user $test"; } else { print "No Match for user $test"; } ``` 2.Should my $authHash be really $authHash{ $something }, I am so confused on this Edit: After some reading tips, still attempting but no dice, any help would be greatly appreciated Edit 2: Can anyone modify my code so that I can understand the answers better? I'm sorry I can't seem to get this to work at all, I have been trying for hours and I really want to know the correct way to do this, I can post my various tries but I feel that will be a waste of real estate.