Thanks Kevin, that did it.

Mark G.

----- Original Message ----- 
From: "Kevin Pfeiffer" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Sunday, June 29, 2003 5:45 AM
Subject: Re: refs to hashes


> In article <[EMAIL PROTECTED]>, Mark G wrote:
>
> > Hi All,
> >
> > I need to read in some data and put it into a data structure ex:
> >
> > knife     #product name
> > 3          #quantity
> > 1          #price
> >
> > I would like to hold in as a hash referancing other hashes ex:
> > $ref=read_data();
> >
> >
> >
> > sub read_data{
> > open $RD,"< value.dat" || print "could not process request\n";
> >
> > while( chomp ( $product   = <$RD> ) ){
> > chomp ( $quantity = <$RD> );
> > chomp ( $price = <$RD> );
> >
> >  $inventory{$product}={
> >    quantity=> $quantityr,
> >    price => $price
> >   };
> > }
> >
> > return \%inventory;
> > }
> >
> > now how can I access the elements for example:
> >
> > foreach( sort keys %{ $ptr } ){
> >      print the quantity and price of each product
> >      if a given product exist print its price
> > }
>
> Here is what I came up with:
> sub price_list {
>    my $ref = shift;
>    my $format = "%15s    %4s    %5.2f \n";
>    printf "%15s     %4s    %5s\n\n", "Product Name", "Qty.", "Price";
>    foreach my $product ( sort keys %{ $ref } ){
>
>       printf $format, $product, $ref->{$product}{quantity},
$ref->{$product}{price};
>
>       # If ea. product has lots of attributes, this add'l loop pulls the
>       # values for all keys, but you might have to modify order (store
>       # attribute names in an ordered array)?
>       #  my @line;
>       #  push @line, $product;
>       #  foreach( keys %{ $ref->{$product} } ){
>       #     push @line, $ref->{$product}{ $_ };
>       #  }
>       #  printf $format, @line;
>
>    } # end of product loop
> }
>
> (Disclaimer - just trying this myself - no warranty)
> -- 
> Kevin Pfeiffer
>
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to