shouldnt this evaluate in a scalar context???

Discussion in 'Perl Misc' started by dutone, Jul 1, 2004.

  1. dutone

    dutone Guest

    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?
    dutone, Jul 1, 2004
    #1
    1. Advertising

  2. dutone

    Paul Lalli Guest

    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
    Paul Lalli, Jul 1, 2004
    #2
    1. Advertising

  3. 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
    Gunnar Hjalmarsson, Jul 1, 2004
    #3
  4. 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
    John W. Krahn, Jul 2, 2004
    #4
  5. dutone

    Anno Siegel Guest

    Gunnar Hjalmarsson <> 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
    Anno Siegel, Jul 2, 2004
    #5
  6. 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
    Gunnar Hjalmarsson, Jul 2, 2004
    #6
  7. 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
    John W. Krahn, Jul 2, 2004
    #7
  8. 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
    Gunnar Hjalmarsson, Jul 2, 2004
    #8
  9. dutone

    dutone Guest

    Thanks for all the input everyone <:|
    dutone, Jul 2, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. guenther schoebel
    Replies:
    2
    Views:
    6,485
    Eric Cartman
    Oct 10, 2003
  2. A. Anderson

    Null Argument shouldnt be?

    A. Anderson, Jun 28, 2007, in forum: C++
    Replies:
    8
    Views:
    298
    Thomas J. Gritzan
    Jun 28, 2007
  3. Clint Olsen
    Replies:
    6
    Views:
    355
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  4. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    163
    Arndt Jonasson
    Jan 27, 2005
  5. eli m
    Replies:
    30
    Views:
    329
    Frank Millman
    Jan 25, 2013
Loading...

Share This Page