Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > converting a hash where the value is a list of lists to just a list

Reply
Thread Tools

converting a hash where the value is a list of lists to just a list

 
 
Lynn
Guest
Posts: n/a
 
      09-09-2005
Hi All,

I have a hash where the values are a list of lists. What I would like to do
is
convert this to a hash where the value is just a reference to an array that
contains
the list of lists. Below is my attempt to convert this to what I want, the
problem
is that I am loosing some people in the process!

use strict;
use warnings;
use Data:umper;
my %people = (
'hillrich' => [ [5308125], [2053628], [5312468], [5312492] ],
'hsieh' => [ [5312182], [5312613], [5312517] ],
'prakash' => [],
'florencb' => [
[1420688], [1420596], [5312242], [5306884], [5305217], [5248521],
],
'x_shukl' => []
);
my %new_format_people=();
foreach my $person(keys %people) {
foreach my $item (@{ $people{$person} }) {
for ( @$item) {
push @{$new_format_people{$person}},$_;
}
}
}

print Dumper(\%new_format_people);

output is:
$VAR1 = {
'hillrich' => [
5308125,
2053628,
5312468,
5312492
],
'hsieh' => [
5312182,
5312613,
5312517
],
'florencb' => [
1420688,
1420596,
5312242,
5306884,
5305217,
5248521
]
};

I lost prakash and x_shukl in the conversion process. How can I keep these
people
in the new hash I am creating?

Thanks

Lynn


 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      09-09-2005
Lynn wrote:

> I have a hash where the values are a list of lists.


It's important to get the terminology correct. The values are
references to arrays of references to single-item arrays. (Contrary to
the naming of the perllol perldoc, there is no such thing as a "list of
lists").

> What I would like to do is
> convert this to a hash where the value is just a reference to an array that
> contains the list of lists.


It looks to me as though what you wanted to do is convert the hash so
that the value is a reference to an array that contains all of the
values of the "inner" arrays.

> Below is my attempt to convert this to what I want, the problem
> is that I am loosing some people in the process!
>
> use strict;
> use warnings;
> use Data:umper;
> my %people = (
> 'hillrich' => [ [5308125], [2053628], [5312468], [5312492] ],
> 'hsieh' => [ [5312182], [5312613], [5312517] ],
> 'prakash' => [],
> 'florencb' => [
> [1420688], [1420596], [5312242], [5306884], [5305217], [5248521],
> ],
> 'x_shukl' => []
> );
> my %new_format_people=();
> foreach my $person(keys %people) {
> foreach my $item (@{ $people{$person} }) {
> for ( @$item) {
> push @{$new_format_people{$person}},$_;


You're only adding any values to the %new_format_people hash within the
loop that goes through the existing list. If there are no values to
iterate over, no value will be added to the new hash.

> }
> }
> }


> I lost prakash and x_shukl in the conversion process. How can I keep these
> people in the new hash I am creating?


I see two options. The least modification to your code would simply be
to explicitly add an empty array reference before attempting to copy
over the "inner" array values:

$new_format_people{$person} = [ ]; #this goes before the innermost for
loop

Alternatively, you could replace both inner for loops with one map
statement:
$new_format_people{$person} = [ map { $_->[0] } @{$people{$person}} ];

This creates a new entry in %new_format_people where the key is the
current person and the value is an array ref consisting of all the
values from "inner" arrays.

To make it more general, in case any of your "inner" arrays ever have
more than one element, replace $_->[0] with the entire array: @$_

Hope this helps,
Paul Lalli

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      09-09-2005
Lynn wrote:
>
> I have a hash where the values are a list of lists. What I would like to do
> is convert this to a hash where the value is just a reference to an array
> that contains the list of lists. Below is my attempt to convert this to what
> I want, the problem is that I am loosing some people in the process!
>
> use strict;
> use warnings;
> use Data:umper;
> my %people = (
> 'hillrich' => [ [5308125], [2053628], [5312468], [5312492] ],
> 'hsieh' => [ [5312182], [5312613], [5312517] ],
> 'prakash' => [],
> 'florencb' => [
> [1420688], [1420596], [5312242], [5306884], [5305217], [5248521],
> ],
> 'x_shukl' => []
> );
> my %new_format_people=();
> foreach my $person(keys %people) {
> foreach my $item (@{ $people{$person} }) {
> for ( @$item) {
> push @{$new_format_people{$person}},$_;
> }
> }
> }
>
> print Dumper(\%new_format_people);


$ perl -e'
use Data:umper;
my %people = (
hillrich => [ [5308125], [2053628], [5312468], [5312492] ],
hsieh => [ [5312182], [5312613], [5312517] ],
prakash => [],
florencb => [ [1420688], [1420596], [5312242], [5306884], [5305217],
[5248521], ],
x_shukl => [],
);
my %new_format_people = %people;
$_ = [ map ref() ? @$_ : $_, @$_ ] for values %new_format_people;
print Dumper \%new_format_people;
'
$VAR1 = {
'hillrich' => [
5308125,
2053628,
5312468,
5312492
],
'hsieh' => [
5312182,
5312613,
5312517
],
'florencb' => [
1420688,
1420596,
5312242,
5306884,
5305217,
5248521
],
'prakash' => [],
'x_shukl' => []
};




John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
Lynn
Guest
Posts: n/a
 
      09-09-2005
Hi Paul,

Paul Lalli wrote:
> Lynn wrote:
>
>> I have a hash where the values are a list of lists.

>
> It's important to get the terminology correct. The values are
> references to arrays of references to single-item arrays. (Contrary
> to the naming of the perllol perldoc, there is no such thing as a
> "list of lists").


Ok

>
>> What I would like to do is
>> convert this to a hash where the value is just a reference to an
>> array that contains the list of lists.

>
> It looks to me as though what you wanted to do is convert the hash so
> that the value is a reference to an array that contains all of the
> values of the "inner" arrays.


yes, that is exactly what I wanted to do. Sorry for the confusion.
>

(snipped)
>
> You're only adding any values to the %new_format_people hash within
> the loop that goes through the existing list. If there are no values
> to iterate over, no value will be added to the new hash.


I see that now. Thanks for pointing it out.

>
>> I lost prakash and x_shukl in the conversion process. How can I keep
>> these people in the new hash I am creating?

>
> I see two options. The least modification to your code would simply
> be to explicitly add an empty array reference before attempting to
> copy over the "inner" array values:
>
> $new_format_people{$person} = [ ]; #this goes before the innermost
> for loop
>
> Alternatively, you could replace both inner for loops with one map
> statement:
> $new_format_people{$person} = [ map { $_->[0] } @{$people{$person}} ];
>
> This creates a new entry in %new_format_people where the key is the
> current person and the value is an array ref consisting of all the
> values from "inner" arrays.


This is exactly what I need

> Hope this helps,


Thanks for all of your help Paul!



 
Reply With Quote
 
Lynn
Guest
Posts: n/a
 
      09-09-2005
Hi John,

John W. Krahn wrote:
> Lynn wrote:
>>

(my stuff snipped)

>
> $ perl -e'
> use Data:umper;
> my %people = (
> hillrich => [ [5308125], [2053628], [5312468], [5312492] ],
> hsieh => [ [5312182], [5312613], [5312517] ],
> prakash => [],
> florencb => [ [1420688], [1420596], [5312242], [5306884],
> [5305217], [5248521], ],
> x_shukl => [],
> );
> my %new_format_people = %people;
> $_ = [ map ref() ? @$_ : $_, @$_ ] for values %new_format_people;
> print Dumper \%new_format_people;


Wow, I need to study this! Thanks a lot

> John



 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-12-2005
Lynn <> wrote in comp.lang.perl.misc:
> Hi John,
>
> John W. Krahn wrote:
> > Lynn wrote:
> >>

> (my stuff snipped)
>
> >
> > $ perl -e'
> > use Data:umper;
> > my %people = (
> > hillrich => [ [5308125], [2053628], [5312468], [5312492] ],
> > hsieh => [ [5312182], [5312613], [5312517] ],
> > prakash => [],
> > florencb => [ [1420688], [1420596], [5312242], [5306884],
> > [5305217], [5248521], ],
> > x_shukl => [],
> > );
> > my %new_format_people = %people;
> > $_ = [ map ref() ? @$_ : $_, @$_ ] for values %new_format_people;
> > print Dumper \%new_format_people;

>
> Wow, I need to study this! Thanks a lot


Here is a way to do it in place:

for ( values %people ) {
$_ = $_->[ 0] for @$_;
}

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
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#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
hash key to var name of value hash key value Une bévue Ruby 5 08-10-2006 04:05 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
converting lists to strings to lists robin Python 10 04-12-2006 04:58 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57