develooper Front page | perl.libwww | Postings from April 2001

Re: How do I build a associative array from a dyanamic list

Thread Previous | Thread Next
From:
Michael A. Chase
Date:
April 11, 2001 12:53
Subject:
Re: How do I build a associative array from a dyanamic list
Message ID:
002701c0c2c0$e0d070c0$2531273f@ca.boeing.com
You just assign more key/value pairs to an interior hash.  Or push values
onto an interior array.  Any of those values could be an array or hash
reference.
--
Mac :})
** I normally forward private database questions to the DBI mail lists. **
Give a hobbit a fish and he'll eat fish for a day.
Give a hobbit a ring and he'll eat fish for an age.
----- Original Message -----
From: "Ben Goswami" <ben.goswami@infospace.com>
To: "'John Stracke'" <francis@ecal.com>; <libwww@perl.org>
Sent: Wednesday, April 11, 2001 12:16 PM
Subject: RE: How do I build a associative array from a dyanamic list


> Thanks, but I still have not the answer.  my question is :
>
> here is the data source which is an xml:
>
> <response>
> <cart>
> <item><id> = 11</item>
> <item>id = 12</item>
> <item>id = 13</item>
> </cart>
> <shipping></shipping>
> </response>
>
> I've to build an nested assoicative array
> (like: %response = ( cart-> { item1 => 11,
> item2 = 12,
> item3 = 13},
>    shipping -> ...
> );
>
>
> from this xmls after parsing.  but the number of items are not fixed. Even
> though I know how many items are there before building the associative
array
> as I cannot put it thru a loop and keep 'adding' to the associative array
> because
> ASSOCIATIVE ARRAY DOES NOT SUPPORT THE 'ADD' OR 'PUSH' FUNCTION LIKE
REGULAR
> ARRAY.
>
> any idea how to solve this

I would probably store these things like this:

my %tags = (
   'response' => [
      {
         'cart' => [
            {
                'item' => [
                   "id = 11", # $tags{'response'}[0]{'cart'}[0]{'item'}[0]
                   "id = 12", # $tags{'response'}[0]{'cart'}[0]{'item'}[1]
                   "id = 13", # $tags{'response'}[0]{'cart'}[0]{'item'}[2]
                ], # $tags{'response'}[0]{'cart'}[0]{'item'}
            }, # $tags{'response'}[0]{'cart'}[0]
            # {}, # would be $tags{'response'}[0]{'cart'}[1]
         ], # $tags{'response'}[0]{'cart'}
         'shipping' => [
            "", # $tags{'response'}[0]{'shipping'}[0]
         ], # $tags{'response'}[0]{'shipping'}
      }, # $tags{'response'}[0]
      #{} # would be $tags{'response'}[1]
   ], # $tags{'response'}
   # 'xxx' => [], # would be $tags{'xxx'}
);

# This is equivalent to:
$tags{'response'}[0]{'cart'}[0]{'item'}[0] = "id = 11";
$tags{'response'}[0]{'cart'}[0]{'item'}[1] = "id = 12";
$tags{'response'}[0]{'cart'}[0]{'item'}[2] = "id = 13";
$tags{'response'}[0]{'shipping'}[0] = "";

# or
my $item_subset = $tags{'response'}[0]{'cart'}[0]{'item'};
push @{$item_subset}, "id = 11", "id = 12", "id = 13";
$tags{'response'}[0]{'shipping'}[0] = "";

You may want to download some of the XML parsing and tree modules from CPAN.
1


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About