shouldnt this evaluate in a scalar context???

D

dutone

my %h = (roach=>'raid',slut=>'fun');
my @a = qw(roach slut);
print "YEA!!" if @h{@a} == @a

Since @a is a list and evals to 2 in scalar context...
and @h{@a} returns a list, but this list doesnt go scalar.
What I was trying to do is see if all the keys in a array
(list,whatever)
exist (not in the 'exists()' scence) in a hash. Of course there are
alot of ways to do this, but i figured this method would work. Any of
you perl thugs have any ideas?
 
P

Paul Lalli

my %h = (roach=>'raid',slut=>'fun');
my @a = qw(roach slut);
print "YEA!!" if @h{@a} == @a

Since @a is a list and evals to 2 in scalar context...
and @h{@a} returns a list, but this list doesnt go scalar.

You've made a rather classic incorrect assumption. You assume that any
list of values in scalar context returns the number of values in that
list. This is untrue. An array evaluated in scalar context does return
the size of that array. A list in scalar context (such as the list
returned by the hash slie @h{@a} returns the last element of that list.

What I was trying to do is see if all the keys in a array
(list,whatever)

Not 'whatever'. They are distinct entities. You should try to learn the
difference between them.
exist (not in the 'exists()' scence) in a hash. Of course there are
alot of ways to do this, but i figured this method would work. Any of
you perl thugs have any ideas?

The keys function is one that returns a list of hash keys in list context,
and the number of hash keys in scalar context:

print 'Yea!!' if keys %h == @a;

Hope this helps,
Paul Lalli
 
G

Gunnar Hjalmarsson

dutone said:
my %h = (roach=>'raid',slut=>'fun');
my @a = qw(roach slut);
print "YEA!!" if @h{@a} == @a

Since @a is a list and evals to 2 in scalar context...

It does not evaluate to 2 in scalar context since it is a list. It
does so because it is an array.
and @h{@a} returns a list, but this list doesnt go scalar.

Yes it does. A list returns the last element in scalar context, and
that is what happens. Perl would have told you so if you had had
warnings enabled.

Please use warnings before posting about code that does not output
what you had expected.
What I was trying to do is see if all the keys in a array
(list,whatever)

The distinction is important in this case!
Any of you perl thugs have any ideas?

print "YEA!!" if @{ [@h{@a}] } == @a;
 
J

John W. Krahn

dutone said:
my %h = (roach=>'raid',slut=>'fun');
my @a = qw(roach slut);
print "YEA!!" if @h{@a} == @a

Since @a is a list and evals to 2 in scalar context...

@a is an array, not a list.

perldoc -q "What is the difference between a list and an array"

and @h{@a} returns a list, but this list doesnt go scalar.

@h{@a} is a hash slice and like the comma operator returns the
right-most element in scalar context.

What I was trying to do is see if all the keys in a array
(list,whatever)
exist (not in the 'exists()' scence) in a hash. Of course there are
alot of ways to do this, but i figured this method would work. Any of
you perl thugs have any ideas?

The only way that I can think of at the moment is to use exists.

print "YEA!!" if @a == grep exists $h{$_}, @a;



John
 
A

Anno Siegel

Gunnar Hjalmarsson said:
dutone said:
my %h = (roach=>'raid',slut=>'fun');
my @a = qw(roach slut);
print "YEA!!" if @h{@a} == @a

Since @a is a list and evals to 2 in scalar context...

It does not evaluate to 2 in scalar context since it is a list. It
does so because it is an array.
and @h{@a} returns a list, but this list doesnt go scalar.

Yes it does. A list returns the last element in scalar context, and
that is what happens. Perl would have told you so if you had had
warnings enabled.

Please use warnings before posting about code that does not output
what you had expected.
What I was trying to do is see if all the keys in a array
(list,whatever)

The distinction is important in this case!
Any of you perl thugs have any ideas?

print "YEA!!" if @{ [@h{@a}] } == @a;

@a = qw( gaga gogo gigi);
print "YEA!!" if @{ [@h{@a}] } == @a;

YEA!!

Still not much of a test. The whole idea doesn't seem to work very
well.

Anno
 
G

Gunnar Hjalmarsson

Anno said:
Gunnar said:
print "YEA!!" if @{ [@h{@a}] } == @a;

@a = qw( gaga gogo gigi);
print "YEA!!" if @{ [@h{@a}] } == @a;

YEA!!

Still not much of a test. The whole idea doesn't seem to work
very well.

Well, it keeps you happy by printing "YEA!!" all the time. :)

Besides that, you are right, of course... Thanks for the correction!

How about this as 'the ultimate solution' to this problem:

print "YEA!!" if @a == grep defined, @h{@a};
 
J

John W. Krahn

Gunnar said:
Anno said:
Gunnar said:
print "YEA!!" if @{ [@h{@a}] } == @a;

@a = qw( gaga gogo gigi);
print "YEA!!" if @{ [@h{@a}] } == @a;

YEA!!

Still not much of a test. The whole idea doesn't seem to work
very well.

Well, it keeps you happy by printing "YEA!!" all the time. :)

Besides that, you are right, of course... Thanks for the correction!

How about this as 'the ultimate solution' to this problem:

print "YEA!!" if @a == grep defined, @h{@a};

That compares the hash keys in @a to the hash values in @h{@a} which is
probably not what the OP wants.


John
 
G

Gunnar Hjalmarsson

John said:
That compares the hash keys in @a to the hash values in @h{@a}
which is probably not what the OP wants.

The OP said: "What I was trying to do is see if all the keys in a
array (list,whatever) exist (not in the 'exists()' scence) in a hash."

I noticed that you suggested:

print "YEA!!" if @a == grep exists $h{$_}, @a;

but considering OP's problem description I found it reasonable to
assume that he meant to check that the hash contains defined values
for all the keys in @a.

Only OP can tell who is right. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top