Click here to get back home

array of arrays to hash of hashes

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
array of arrays to hash of hashes Jeff 09-06-2008
Get Chitika Premium
Posted by Jeff on September 6, 2008, 11:21 pm
Please log in for more thread options


Hi all,

I have a category / subcategory file that looks like this

cereal
cereal:cold:cheerios
cereal:cold:corn flakes
cereal:hot:oatmeal
cereal:hot:cream of wheat
bread
bread:dakine sweet bread
bread:wheat
bread:white

...... Anyway you get the picture

I read the file in and place into array of arrays

open FILE, "$file";
my @file = (<FILE>);
close FILE;

my @a;
foreach (@file){
        my @line = split ':', $_;
        push (@a, \@line);
}

I want to put this into a hash of hashes where the last element would be
equal to 1. something like this
$cereal->-> =1;
or $bread-> = 1;

if later on down the line the file changes so that there is more
subcategories, the 1 would be replaced by a new hash like this

$cereal->->-> =1;
$cereal->-> = 1;

I'm having trouble writing a function to do this.
Any help or ideas would be appreciated

Posted by Mark Clements on September 7, 2008, 3:27 am
Please log in for more thread options


Jeff wrote:
> Hi all,
>
> I have a category / subcategory file that looks like this
>
> cereal
> cereal:cold:cheerios
> cereal:cold:corn flakes
> cereal:hot:oatmeal
> cereal:hot:cream of wheat
> bread
> bread:dakine sweet bread
> bread:wheat
> bread:white
>
> ...... Anyway you get the picture
>
> I read the file in and place into array of arrays
>
> open FILE, "$file";
> my @file = (<FILE>);
> close FILE;
>
> my @a;
> foreach (@file){
> my @line = split ':', $_;
> push (@a, \@line);
> }
>
> I want to put this into a hash of hashes where the last element would be
> equal to 1. something like this
> $cereal->-> =1;
> or $bread-> = 1;
>
> if later on down the line the file changes so that there is more
> subcategories, the 1 would be replaced by a new hash like this
>
> $cereal->->-> =1;
> $cereal->-> = 1;
>
> I'm having trouble writing a function to do this.
> Any help or ideas would be appreciated
The most obvious way of doing it is to use a recursive function (google
"recursion") to walk the data structure each time a new item is inserted
(ie for each new line). You probably want something a bit like this
(insert_item is the recursive function, if that isn't obvious):

mark@hermes:~$ cat cereal.pl
#!/usr/bin/perl
#
use warnings;
use strict;

use Data::Dumper;

my $root = {};

while(my $line = <DATA> ){
chomp $line;
my @items = split /:/,$line;

insert_item( $root, @items );
}

print Dumper $root;

sub insert_item {
my $current = shift;
my $nextkey = shift;
my @items = @_;

if(@items){
my $newsubtree;
if(ref $current->){
$newsubtree = $current->{ $nextkey };
}else{
$newsubtree = {};
$current->{ $nextkey } = $newsubtree;
}
insert_item( $newsubtree, @items );
}else{
$current->{ $nextkey } = 1;
}
}

__END__
cereal
cereal:cold:cheerios
cereal:cold:corn flakes
cereal:hot:oatmeal
cereal:hot:cream of wheat
bread
bread:dakine sweet bread
bread:wheat
bread:white
mark@hermes:~$ perl cereal.pl
$VAR1 = {
'bread' => {
'white' => 1,
'wheat' => 1,
'dakine sweet bread' => 1
},
'cereal' => {
'cold' => {
'cheerios' => 1,
'corn flakes' => 1
},
'hot' => {
'cream of wheat' => 1,
'oatmeal' => 1
}
}
};


Mark


Posted by Jeff on September 7, 2008, 10:56 pm
Please log in for more thread options


Thanks a million..

I learned something here and it's great



Mark Clements wrote:
> Jeff wrote:
>> Hi all,
>>
>> I have a category / subcategory file that looks like this
>>
>> cereal
>> cereal:cold:cheerios
>> cereal:cold:corn flakes
>> cereal:hot:oatmeal
>> cereal:hot:cream of wheat
>> bread
>> bread:dakine sweet bread
>> bread:wheat
>> bread:white
>>
>> ...... Anyway you get the picture
>>
>> I read the file in and place into array of arrays
>>
>> open FILE, "$file";
>> my @file = (<FILE>);
>> close FILE;
>>
>> my @a;
>> foreach (@file){
>> my @line = split ':', $_;
>> push (@a, \@line);
>> }
>>
>> I want to put this into a hash of hashes where the last element would be
>> equal to 1. something like this
>> $cereal->-> =1;
>> or $bread-> = 1;
>>
>> if later on down the line the file changes so that there is more
>> subcategories, the 1 would be replaced by a new hash like this
>>
>> $cereal->->-> =1;
>> $cereal->-> = 1;
>>
>> I'm having trouble writing a function to do this.
>> Any help or ideas would be appreciated
> The most obvious way of doing it is to use a recursive function (google
> "recursion") to walk the data structure each time a new item is inserted
> (ie for each new line). You probably want something a bit like this
> (insert_item is the recursive function, if that isn't obvious):
>
> mark@hermes:~$ cat cereal.pl
> #!/usr/bin/perl
> #
> use warnings;
> use strict;
>
> use Data::Dumper;
>
> my $root = {};
>
> while(my $line = <DATA> ){
> chomp $line;
> my @items = split /:/,$line;
>
> insert_item( $root, @items );
> }
>
> print Dumper $root;
>
> sub insert_item {
> my $current = shift;
> my $nextkey = shift;
> my @items = @_;
>
> if(@items){
> my $newsubtree;
> if(ref $current->){
> $newsubtree = $current->{ $nextkey };
> }else{
> $newsubtree = {};
> $current->{ $nextkey } = $newsubtree;
> }
> insert_item( $newsubtree, @items );
> }else{
> $current->{ $nextkey } = 1;
> }
> }
>
> __END__
> cereal
> cereal:cold:cheerios
> cereal:cold:corn flakes
> cereal:hot:oatmeal
> cereal:hot:cream of wheat
> bread
> bread:dakine sweet bread
> bread:wheat
> bread:white
> mark@hermes:~$ perl cereal.pl
> $VAR1 = {
> 'bread' => {
> 'white' => 1,
> 'wheat' => 1,
> 'dakine sweet bread' => 1
> },
> 'cereal' => {
> 'cold' => {
> 'cheerios' => 1,
> 'corn flakes' => 1
> },
> 'hot' => {
> 'cream of wheat' => 1,
> 'oatmeal' => 1
> }
> }
> };
>
>
> Mark
>

Similar ThreadsPosted
FAQ 4.68: How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays? December 6, 2004, 6:03 pm
FAQ 4.68 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays? February 7, 2005, 12:03 am
FAQ 4.68 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays? April 18, 2005, 5:03 pm
FAQ 4.68 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays? July 4, 2005, 4:03 pm
FAQ 4.68 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays? October 3, 2005, 4:03 pm
FAQ 4.68 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays? January 4, 2006, 11:03 pm
FAQ 4.68 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays? April 25, 2006, 3:03 pm
FAQ 4.68 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays? July 5, 2006, 9:03 pm
FAQ 4.68 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays? August 15, 2006, 9:03 am
FAQ 4.68 How can I make the Perl equivalent of a C structure/C++ class/hash or array of hashes or arrays? October 2, 2006, 9:03 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap