Hash of arrays & 'values' function

B

Ben Tisdall

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.
 
B

Brian McCauley

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;
 
B

Ben Tisdall

Gunnar said:
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.
 
T

Tad McClellan

Ben Tisdall said:
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...
 
A

anno4000

Ben Tisdall said:
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
 
B

Ben Tisdall

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 :)
 
A

anno4000

Ben Tisdall said:
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;
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
 
B

Ben Tisdall

The standard way:

map @$_, values %userlist;


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.
 

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,999
Messages
2,570,246
Members
46,840
Latest member
BrendanG78

Latest Threads

Top