A
alexbarham
I am working through the Mastering Algorithms with PERL and there is a
section of code I don't quite honestly understand and it is giving me a
compile error of "Can't use an undefined value as a HASH reference".
Here is the code:
#! /usr/bin/perl -w
sub intersection
{
my($i,$sizei) = (0,scalar keys%{$_[0]});
my($j,$sizej);
for($j=1;$j<@_;$j++)
{
$sizej = scalar keys %{$_[$j]};
}
($i,$sizei) = ($j,$sizej) if $sizej < $sizei;
my @intersection = keys %{splice @_,$i,1};
my $set;
while($set = shift)
{
@intersection = grep {exists $set->{$_}} @intersection;
}
my %intersection;
@intersection {@intersection} = ();
return \%intersection;
}
@Cats{qw(cat lion tiger)} = ();
@Asian{qw(tiger panda yak)} = ();
@Striped{qw(zebra tiger)} = ();
@Cats_Asian_Striped = intersection(\%Cats,\%Asian,\%Striped);
print join(" ",keys %{$Cats_Asian_Striped}),"\n";
This should output just tiger (the intersection of all three hashes).
Can someone please explain the syntax of the line:
I have two questions about the code:
1) The following line is where the error is generated:
my @intersection = keys %{splice @_,$i,1);
I understand what a splice does but if I understand correctly, @_
refers to the arguments passed into the subroutine. $i is initialized
to 0 at the beginning of the script and then gets set to the smallest
array. This should be @Striped, which is equal to 2. Thus
splice(@_,2,1) looks to me like it will remove the @Striped from @_??
How can I fix this to eliminate the error?
2) What kind of data structure is the following line:
@Cats{qw(cat lion tiger)} = ();
It start with an array symbol but the parenthese look like it is
initialized to zero elements??
Thank you for your help
section of code I don't quite honestly understand and it is giving me a
compile error of "Can't use an undefined value as a HASH reference".
Here is the code:
#! /usr/bin/perl -w
sub intersection
{
my($i,$sizei) = (0,scalar keys%{$_[0]});
my($j,$sizej);
for($j=1;$j<@_;$j++)
{
$sizej = scalar keys %{$_[$j]};
}
($i,$sizei) = ($j,$sizej) if $sizej < $sizei;
my @intersection = keys %{splice @_,$i,1};
my $set;
while($set = shift)
{
@intersection = grep {exists $set->{$_}} @intersection;
}
my %intersection;
@intersection {@intersection} = ();
return \%intersection;
}
@Cats{qw(cat lion tiger)} = ();
@Asian{qw(tiger panda yak)} = ();
@Striped{qw(zebra tiger)} = ();
@Cats_Asian_Striped = intersection(\%Cats,\%Asian,\%Striped);
print join(" ",keys %{$Cats_Asian_Striped}),"\n";
This should output just tiger (the intersection of all three hashes).
Can someone please explain the syntax of the line:
I have two questions about the code:
1) The following line is where the error is generated:
my @intersection = keys %{splice @_,$i,1);
I understand what a splice does but if I understand correctly, @_
refers to the arguments passed into the subroutine. $i is initialized
to 0 at the beginning of the script and then gets set to the smallest
array. This should be @Striped, which is equal to 2. Thus
splice(@_,2,1) looks to me like it will remove the @Striped from @_??
How can I fix this to eliminate the error?
2) What kind of data structure is the following line:
@Cats{qw(cat lion tiger)} = ();
It start with an array symbol but the parenthese look like it is
initialized to zero elements??
Thank you for your help