----- Original Message ----- From: "Chris Charley" <[EMAIL PROTECTED]>
To: "klute" <[EMAIL PROTECTED]>
Sent: Tuesday, July 10, 2007 7:17 PM
Subject: Re: Help needed created this data structure



----- Original Message ----- From: "klute" <[EMAIL PROTECTED]>
Newsgroups: perl.beginners
To: "klute" <[EMAIL PROTECTED]>; <beginners@perl.org>
Sent: Tuesday, July 10, 2007 5:37 PM
Subject: Re: Help needed created this data structure



--- klute <[EMAIL PROTECTED]> wrote:


--- "D. Bolliger" <[EMAIL PROTECTED]> wrote:
> Hello Klute
> > (please don't top post to keep the conversation
> readable)
> > The following script extracts the information out
of
> your sample data.
> There are no checks if the data format is
"correct"
> (nesting order, additional > text). > > It does not result in an array of hashes, but in a
> single hash.
> Modify it if needed :-)
> > Dani > > > #!/usr/bin/perl > > use strict;
> use warnings;
> > use Data::Dumper; > > my %data; > > # holds the current first and second level
> #
> my ($parent_group, $aff_group);
> > while (<DATA>) { > > # a loop block, so we can use next
>    {
>       # skip blank lines
>       /^\s*$/
>          and next;
> > # record current first level
>       /^A.*?: (.*)/ and $parent_group=$1
>          and next;
> > # record current second level
>       /^\s+->.*?: (.*)/ and $aff_group=$1
>          and next;
> > # fill %data, with completed three levels
>       /-->.*?: (\d+).*?: (\w+)/
>          and
> $data{$parent_group}{$aff_group}{$1}=$2;
>    }
> > } > > print Data::Dumper::Dumper \%data; > > > __DATA__
> Affiliate Parent Group: Google
>   -> Affiliate Group: Google Advertiser
>      --> Affiliate (Aff Id: 1, Aff Name: Frank)
>      --> Affiliate (Aff Id: 2, Aff Name: Mary)
> > -> Affiliate Group: Google Publisher
>      --> Affiliate (Aff Id: 3, Aff Name: Lori)
>      --> Affiliate (Aff Id: 4, Aff Name: Mike)
> > > Affiliate Parent Group: Yahoo
>   -> Affiliate Group: Yahoo Advertiser
>      --> Affiliate (Aff Id: 5, Aff Name: Marlene)
>      --> Affiliate (Aff Id: 6, Aff Name: Larry)
>   -> Affiliate Group: Yahoo Publisher
>      --> Affiliate (Aff Id: 7, Aff Name: Alex)
>      --> Affiliate (Aff Id: 8, Aff Name: Glenn)
>
Thanks guys for your replies. It works! So what data
structure am I dealing with here?
$data{$parent_group}{$aff_group}{$1}=$2;

Best,
James


Also, I am not quite sure how to iterate through this
structure and print its contents without using Dumper.
Can anyone help?

Thanks very much!
James


Hello James,

To understand hash references, read the docs.

the perldsc manpage

This is a hash of hash of hash.
One way to get the info could be:

for my $parent (keys %data) {
for my $group (keys %{ $data{$parent} }) {
 my $recs = $data{$parent}{$group};
 while (my ($id, $name) = each %$recs) {
  print "$parent - $group - $id - $name\n";
 }
}
}

Chris

Sent to klute by mistake. Here it is for the list.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to