Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > shouldnt this evaluate in a scalar context???

Reply
Thread Tools

shouldnt this evaluate in a scalar context???

 
 
dutone
Guest
Posts: n/a
 
      07-01-2004
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?
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      07-01-2004
On Thu, 1 Jul 2004, dutone wrote:

> 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



 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-01-2004
dutone wrote:
>
> 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;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      07-02-2004
dutone wrote:
>
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      07-02-2004
Gunnar Hjalmarsson <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> dutone wrote:
> >
> > 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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-02-2004
Anno Siegel wrote:
> Gunnar Hjalmarsson wrote:
>>
>> 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};

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      07-02-2004
Gunnar Hjalmarsson wrote:
>
> Anno Siegel wrote:
> > Gunnar Hjalmarsson wrote:
> >>
> >> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-02-2004
John W. Krahn wrote:
> Gunnar Hjalmarsson wrote:
>> 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.


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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
dutone
Guest
Posts: n/a
 
      07-02-2004
Thanks for all the input everyone <
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Null Argument shouldnt be? A. Anderson C++ 8 06-28-2007 09:03 PM
Why shouldnt I get Vista (or which version) Nik Coughlin NZ Computing 27 02-09-2007 12:37 AM
Replace scalar in another scalar Mark Perl Misc 4 01-27-2005 02:48 PM
Shorthand for($scalar) loops and resetting pos($scalar) Clint Olsen Perl Misc 6 11-13-2003 12:50 AM
reroute to another url, but the first url must shouldnt change in theadress-mask guenther schoebel HTML 2 10-10-2003 07:44 AM



Advertisments