I hope this is the appropriate forum to ask this question.
assume I have a list like this:
input = ('1', 'hostname1', '1', 'hostname2', '1',
'hostname3', '2',
'hostname10', '2', 'hostname11', '3', 'hostname12');
I want to turn this list into a hash of anonymous
arrays...something
like this I guess.
%hash = (
'1' => ['hostname1', 'hostname2', 'hostname3'],
'2' => ['hostname10', 'hostname11'],
'3' => ['hostname12'],
);
I hope I explained that appropriately.
regards,
J
Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://g
roups.yahoo.com/group/perl-beginner/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http
://groups.yahoo.com/group/perl-beginner/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:perl-beginner-digest@yahoogroups.com
mailto:perl-beginner-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
perl-beginner-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|
On Feb 7, 2007, at 10:58 AM, jslay77 wrote:
> I hope this is the appropriate forum to ask this
question.
>
> assume I have a list like this:
>
> input = ('1', 'hostname1', '1', 'hostname2', '1',
'hostname3', '2',
> 'hostname10', '2', 'hostname11', '3', 'hostname12');
>
> I want to turn this list into a hash of anonymous
arrays...something
> like this I guess.
>
> %hash = (
> '1' => ['hostname1', 'hostname2', 'hostname3'],
> '2' => ['hostname10', 'hostname11'],
> '3' => ['hostname12'],
> );
>
>
>
>
> I hope I explained that appropriately.
> regards,
> J
Why don't you post some code showing your attempt and then
we can
help you if it doesn't do what you expect? Here's a hint,
to
initialize a hash element with an empty array if it doesn't
already
have a value, you can do this:
$hash{$key} ||= [];
I made a subroutine "array_to_hash" taking a list
of values and
returning a hash. My little script also used
List::MoreUtils::natatime to get the pairs from the array,
but that's
not the only way. I also used Data: umper to
produce the output to
visually check the result like this:
input = ('1', 'hostname1', '1', 'hostname2', '1',
'hostname3', '2',
'hostname10', '2', 'hostname11', '3',
'hostname12');
my %hash = array_to_hash input;
print Data: umper-&g
t;Dump([ input, %hash], [qw(*input *hash)]);
input = (
'1',
'hostname1',
'1',
'hostname2',
'1',
'hostname3',
'2',
'hostname10',
'2',
'hostname11',
'3',
'hostname12'
);
%hash = (
'1' => [
'hostname1',
'hostname2',
'hostname3'
],
'3' => [
'hostname12'
],
'2' => [
'hostname10',
'hostname11'
]
);
-Rob
Rob Biedenharn http://agileconsultingl
lc.com
Rob AgileConsultingLLC.com
Unsubscribing info is here: h
ttp://help.yahoo.com/help/us/groups/groups-32.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://g
roups.yahoo.com/group/perl-beginner/
<*> Your email settings:
Individual Email | Traditional
<*> To change settings online go to:
http
://groups.yahoo.com/group/perl-beginner/join
(Yahoo! ID required)
<*> To change settings via email:
mailto:perl-beginner-digest@yahoogroups.com
mailto:perl-beginner-fullfeatured@yahoogroups.com
<*> To unsubscribe from this group, send an email to:
perl-beginner-unsubscribe@yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.c
om/info/terms/
|