Perl的:从子程序返回哈希 [英] Perl: Return hash from subroutine
问题描述
我一直在尝试了几个小时的例子,但我似乎无法把握怎么做我想做的事情。
I have been trying examples for hours but I can't seem to grasp how to do what I want to do.
我想从一个子程序返回哈希,和我猜想的参考是最好的选择。这里就是它变得有点棘手。我要引用如$哈希{$ X}哈希值。我仍然在Perl中的小白:/
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。首先的问题,我用的例子似乎表明它是确定使用$ hashTable中{$登录},我应该使用%hashTable中{$登录}或者它没有关系?下面是code:
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 };
}
然后我要测试这个数据,看看是否登录为present,这里是我的测试方法
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我的$ authHash真的$ authHash {$}的东西,我对这个很困惑
2.Should my $authHash be really $authHash{ $something }, I am so confused on this
编辑:经过一些阅读技巧,仍在试图但没有骰子,任何帮助将大大AP preciated
After some reading tips, still attempting but no dice, any help would be greatly appreciated
编辑2:任何人都可以修改我的code,这样我可以理解的答案更好?对不起,我似乎无法得到这在所有的工作,我一直在努力了几个小时,我真的想知道正确的方式做到这一点,我可以张贴我的各种尝试,但我觉得这将是一种浪费房产。
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.
推荐答案
首先,如评论所说mpapec,使用严格的;使用警告;
。这将赶上最常见的错误,其中最萎靡不振的你问这里的问题(通常提供有关你应该做的,而不是提示)。
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).
现在来回答问题1和问题2:
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 = \\%哈希
要出去,你必须参考哈希的元素,你需要取消对它的引用第一。这通常是用完成 - &GT;
运营商,像这样:
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-&GT; {键}
请注意,您使用 - &GT;
当您从引用( $ hash_ref-&GT启动; {键}
),而不是当你从一个实际的哈希启动( $ {哈希键}
)。
Note that you use ->
when you start from a reference ($hash_ref->{key}
), but not when you start from an actual hash ($hash{key}
).
(如在问题2一个侧面说明,不要用&放preFIX次通话;
- 只需使用 AUTHUSER()
,而不是&放大器; AUTHUSER()
的&放大器;
不再需要在Perl 5 +并有副作用,你通常不希望,所以你不应该在使用它的地方它没有必要的习惯得到的。)
(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.)
有关问题3,如果你只打算数组,你可能也只是循环过来检查一次,检查每一个元素:
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";
}
这篇关于Perl的:从子程序返回哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!