Hash of arrays & 'values' function

Discussion in 'Perl Misc' started by Ben Tisdall, Nov 29, 2006.

  1. Ben Tisdall

    Ben Tisdall Guest

    Hi, hope I'm asking this in the correct place (I'm at the "'Learning
    Perl' was a breeze" but "'Programming Perl' is frequently making my head
    hurt'"stage...)

    Anyhoo, I need to store info about which hosts users are logged into &
    since they might be logged into more than one I'm using a hash of arrays.

    Getting data in & pulling it out based on each key is no problem, but
    what if I want to get a list of all machines currently logged into? I
    know I can iterate over the hash using 'keys' & build a simple array of
    all the hosts like this:

    foreach (keys%userlist) {
    push(@all,@{$userlist{$_}});
    }

    But it seems to me that this is unnecessarily expensive & I should be
    able to use 'values' to get at the list.

    I understand that values%userlist isn't right, but what is?

    TIA.

    --
    Ben Tisdall
    Ben Tisdall, Nov 29, 2006
    #1
    1. Advertising

  2. On Nov 29, 9:03 am, Ben Tisdall <> wrote:
    > Hi, hope I'm asking this in the correct place (I'm at the "'Learning
    > Perl' was a breeze" but "'Programming Perl' is frequently making my head
    > hurt'"stage...)
    >
    > Anyhoo, I need to store info about which hosts users are logged into &
    > since they might be logged into more than one I'm using a hash of arrays.
    >
    > Getting data in & pulling it out based on each key is no problem, but
    > what if I want to get a list of all machines currently logged into? I
    > know I can iterate over the hash using 'keys' & build a simple array of
    > all the hosts like this:
    >
    > foreach (keys%userlist) {
    > push(@all,@{$userlist{$_}});
    > }


    > But it seems to me that this is unnecessarily expensive & I should be
    > able to use 'values' to get at the list.
    >
    > I understand that values%userlist isn't right, but what is?


    Yes it is, but you still need to dereference the array references.

    foreach (values %userlist) {
    push(@all,@$_);
    }

    Or

    my @all = map { @$_ } values %userlist;
    Brian McCauley, Nov 29, 2006
    #2
    1. Advertising

  3. Ben Tisdall wrote:
    > I understand that values%userlist isn't right, but what is?


    If I understood you correctly

    keys %userlist

    is what you want.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Nov 29, 2006
    #3
  4. Ben Tisdall

    Ben Tisdall Guest

    Gunnar Hjalmarsson wrote:
    > Ben Tisdall wrote:
    >> I understand that values%userlist isn't right, but what is?

    >
    > If I understood you correctly
    >
    > keys %userlist
    >
    > is what you want.
    >


    Thanks, but I was curious about the most efficient way to return a list
    consisting of the all values in the nested arrays.

    Thanks to the Brian for his solutions.
    Ben Tisdall, Nov 29, 2006
    #4
  5. Ben Tisdall <> wrote:

    > Hi, hope I'm asking this in the correct place (I'm at the "'Learning
    > Perl' was a breeze" but "'Programming Perl' is frequently making my head
    > hurt'"stage...)



    I've been in that latter stage for over 10 years...


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 29, 2006
    #5
  6. Ben Tisdall

    -berlin.de Guest

    Ben Tisdall <> wrote in comp.lang.perl.misc:
    > Gunnar Hjalmarsson wrote:
    > > Ben Tisdall wrote:
    > >> I understand that values%userlist isn't right, but what is?

    > >
    > > If I understood you correctly
    > >
    > > keys %userlist
    > >
    > > is what you want.
    > >

    >
    > Thanks, but I was curious about the most efficient way to return a list
    > consisting of the all values in the nested arrays.


    You say you want that, but in your OP your said:

    > what if I want to get a list of all machines currently logged into?


    Since the machines are the keys of %userlist you will have to go
    through the keys. There is no way to access the keys from the values.

    If you actually meant to say "...list of all *users* currently logged in"
    that's a different story, but then your OP was really misleading.

    Anno
    -berlin.de, Nov 29, 2006
    #6
  7. Ben Tisdall

    Ben Tisdall Guest

    -berlin.de wrote:

    > Since the machines are the keys of %userlist


    Ah, but I never said they were. The keys are the users.

    I either want a list of the machines a particular user is logged into or
    a list of all machines that are currently logged into. I'd achieved
    this, my question was really a general one about the 'best' way to
    dereference arrays within hashes, as the first respondents correctly
    deduced.
    >
    > If you actually meant to say "...list of all *users* currently logged in"
    > that's a different story, but then your OP was really misleading.
    >

    So it wasn't :)
    Ben Tisdall, Nov 29, 2006
    #7
  8. Ben Tisdall

    -berlin.de Guest

    Ben Tisdall <> wrote in comp.lang.perl.misc:
    > -berlin.de wrote:
    >
    > > Since the machines are the keys of %userlist

    >
    > Ah, but I never said they were. The keys are the users.
    >
    > I either want a list of the machines a particular user is logged into or
    > a list of all machines that are currently logged into. I'd achieved
    > this, my question was really a general one about the 'best' way to
    > dereference arrays within hashes, as the first respondents correctly
    > deduced.


    The standard way:

    map @$_, values %userlist;

    > > If you actually meant to say "...list of all *users* currently logged in"
    > > that's a different story, but then your OP was really misleading.
    > >

    > So it wasn't :)


    It wasn't exactly clear either. In particular, the name %userlist seems
    to indicate that the values represent users in some sense.

    Anno
    -berlin.de, Nov 29, 2006
    #8
  9. Ben Tisdall

    Ben Tisdall Guest

    -berlin.de wrote:
    > Ben Tisdall <> wrote in comp.lang.perl.misc:
    >> -berlin.de wrote:
    >>
    >>> Since the machines are the keys of %userlist

    >> Ah, but I never said they were. The keys are the users.
    >>
    >> I either want a list of the machines a particular user is logged into or
    >> a list of all machines that are currently logged into. I'd achieved
    >> this, my question was really a general one about the 'best' way to
    >> dereference arrays within hashes, as the first respondents correctly
    >> deduced.

    >
    > The standard way:
    >
    > map @$_, values %userlist;
    >
    >>> If you actually meant to say "...list of all *users* currently logged in"
    >>> that's a different story, but then your OP was really misleading.
    >>>

    >> So it wasn't :)

    >
    > It wasn't exactly clear either. In particular, the name %userlist seems
    > to indicate that the values represent users in some sense.


    Ok, I see where you're coming from. Actually the hash started as users
    keyed by machines, hence the name, but then got switched up when I
    realised that a user might be logged into more than one machine (whereas
    the converse would be very unlikely).

    Thanks for your contribution.

    --
    Ben Tisdall
    Ben Tisdall, Nov 29, 2006
    #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. Philipp
    Replies:
    21
    Views:
    1,126
    Philipp
    Jan 20, 2009
  2. rp
    Replies:
    1
    Views:
    516
    red floyd
    Nov 10, 2011
  3. Adam Akhtar
    Replies:
    5
    Views:
    646
    Adam Akhtar
    Mar 25, 2008
  4. Älphä Blüë

    Hash of Hash of Arrays Question

    Älphä Blüë, Jul 18, 2009, in forum: Ruby
    Replies:
    5
    Views:
    667
    Älphä Blüë
    Jul 18, 2009
  5. Tore Aursand
    Replies:
    3
    Views:
    551
    Anno Siegel
    Sep 16, 2003
Loading...

Share This Page