Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Hash of arrays & 'values' function

Reply
Thread Tools

Hash of arrays & 'values' function

 
 
Ben Tisdall
Guest
Posts: n/a
 
      11-29-2006
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
 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      11-29-2006


On Nov 29, 9:03 am, Ben Tisdall <(E-Mail Removed)> 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;

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      11-29-2006
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
 
Reply With Quote
 
Ben Tisdall
Guest
Posts: n/a
 
      11-29-2006
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.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-29-2006
Ben Tisdall <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      11-29-2006
Ben Tisdall <(E-Mail Removed)> 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
 
Reply With Quote
 
Ben Tisdall
Guest
Posts: n/a
 
      11-29-2006
http://www.velocityreviews.com/forums/(E-Mail Removed)-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
 
Reply With Quote
 
anno4000@radom.zrz.tu-berlin.de
Guest
Posts: n/a
 
      11-29-2006
Ben Tisdall <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> (E-Mail Removed)-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
 
Reply With Quote
 
Ben Tisdall
Guest
Posts: n/a
 
      11-29-2006
(E-Mail Removed)-berlin.de wrote:
> Ben Tisdall <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> (E-Mail Removed)-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
 
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
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash of Hash of Arrays Question Älphä Blüë Ruby 5 07-18-2009 07:36 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
hash of arrays - appending to one of the arrays Adam Akhtar Ruby 5 03-25-2008 11:53 PM
Sort a hash based on values in the hash stored as arrays of hashes Tore Aursand Perl Misc 3 09-16-2003 10:14 AM



Advertisments