klute am Montag, 9. Juli 2007 23:04:
> --- Chris Charley <[EMAIL PROTECTED]> wrote:
> > ----- Original Message -----
> > From: "klute" <[EMAIL PROTECTED]>
> > Newsgroups: perl.beginners
> > To: <beginners@perl.org>
> > Sent: Monday, July 09, 2007 4:20 PM
> > Subject: Help needed created this data structure
> >
> > > Hi All,
> > >
> > > I am new to Perl and was hoping to get advice on
> > > creating the following data structure:
> > >
> > > I have an Affiliate Parent Groups, Affiliate
> >
> > Groups,
> >
> > > and Affiliates. Each affiliate has affiliateId,
> > > affiliateName.
> > >
> > > I guess what I'd like to have is an array of
> >
> > hashes
> >
> > > where the array would contain Affiliate Parent
> >
> > Groups.
> >
> > > Each item in the array would contain a hash map
> >
> > with
> >
> > > Affiliate Group name as the key and An array of
> > > Affiliates (each having AffiliateId and
> >
> > AffiliateName)
> >
> > > as the value.
> > >
> > > How would I go about creating such structure and
> > > adding values to it in a loop?
> > >
> > > Any help will be greatly appreciated!
> > >
> > > James
> >
> > Its hard to see what structure you want without some
> > sample data.
> > Send some data as it is in the file.
> >
> > Have you tried any coding yet?
> >
> > Chris
> Hi Chris,
>
> I did try coding this but I am afraid to confuse
> everyone with what I came up with so far. I can paste
> what I have if you feel that it would help.
>
> Here is the sample 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)

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)

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


Reply via email to