jdavis wrote:

Hi Please post to the list.  I did a lot of work on my reply to this, and didn't even 
notice till now that it had not reached the list.

Pasted below:
> On Tue, 2003-04-01 at 13:07, R. Joseph Newton wrote:
> > jdavis wrote:
> > > >  I have a hash. To use this hash with a module I need it in this form...
> > > >
> > > > @data = (
> > > >     ["12am","1am","2am","3am","4am","5am","6am", "7am", "8am"],  ### key
> > > >     [   251,   102,  55,   36,   113,  200,    32,    99,   4],   ###val
> > > > );
> > > > I confused.. How could I get a hash to the above structure
> > > >
> >
> > > I have a hash that i needed to put into a 2d array.
> > > I wound up doing this...
> > >
> > > foreach $key (keys(%temp)){
> > >         push(@graph_key,$key);
> > >         push(@graph_value,$temp{$key});
> > > }
> > >
> > > $data[0] = [EMAIL PROTECTED];
> > > $data[1] = [EMAIL PROTECTED];
> >
> > Nice!  Very nice!
> >
> > > Though Im not quite sure why i need to escape the @?
> > > but it works :)
> >
> > You don't.  In that context, outside of any string, you are taking a reference
to each array.  You will profit greatly by reading up on references.  They are one
of the keys to power programming.
>
> dont put a pointer to the array inside $data[0],
> but actually put the whole @array_object into $data[0] ?
>
> Is that whats happening?

Sort of, except the other way.  In fact, that is not possible to put a whole array
into the element..  Any element of an array can hold only a single scalar.  That is
what I mean about power programming.  Using references allows you to link to
structures that you could never possibly store as elements.  This allows you to
model objects of any degree of complexity on a single hash:

#!perl -w

use strict;
use warnings;

my $full_name = "Robert Joseph Newton";
my $address_part_1 = '000 Surly Lane';
my $address_part_2 = '';
my @children = ({Name => 'Joe', Age => 19},
  {Name => 'Bonnie', Age => 17}, {Name => 'Clyde', Age => 16},
   {Name => 'Floyd', Age => 14,
    Interests => ['Reading', 'Running', 'Rithmetik']});

my  $person_ref = {
  Fullname => $full_name,
  Address => {
    Street => $address_part_1,
    Street2 => $address_part_2
  },
  Children => [EMAIL PROTECTED]
};

foreach (keys %{$person_ref}) {
  print "$_\n";
  my $referent = $person_ref->{$_};
  if (ref($referent)) {
    if ($referent =~ /^ARRAY/) {
      plumb_array ($referent, 2);
    } elsif ($referent =~ /^HASH/) {
      plumb_hash ($referent, 2);
    }
  } else {
    print "  $person_ref->{$_}\n";
  }
}
print "Are the attributes and values of this person.\n\n";


sub plumb_array {
  my ($array_ref, $indent) = @_;
  foreach (@{$array_ref}) {
    my $element = $_;
    my $ref_class = ref($element);
    if ($ref_class) {
      if ($ref_class =~ /^ARRAY/) {
        plumb_array($element, $indent + 2);
      } elsif ($ref_class =~ /^HASH/) {
        plumb_hash($element, $indent + 2);
      }
    } else {
      print ' ' x ($indent) . "$element\n";
    }
  }
  print "\n";
}

sub plumb_hash {
  my ($hash_ref, $indent) = @_;
  foreach (keys %{$hash_ref}) {
    my $element = $hash_ref->{$_};
    my $ref_class = ref($element);
    if ($ref_class) {
      if ($ref_class =~ /^ARRAY/) {
        print ' ' x ($indent) . "$_:\n";
        plumb_array($element, $indent + 2);
      } elsif ($ref_class =~ /^HASH/) {
        print ' ' x ($indent) . "$_:\n";
        plumb_hash($element, $indent + 2);
      }
    } else {
      print ' ' x ($indent) . "$_: $element\n";
    }
  }
  print "\n";
}

OUTPUT:

Howdy, podner! E:\d_drive\perlStuff>person_hash.pl
Address
  Street2:
  Street: 000 Surly Lane

Children
    Age: 19
    Name: Joe

    Age: 17
    Name: Bonnie

    Age: 16
    Name: Clyde

    Age: 14
    Interests:
      Reading
      Running
      Rithmetik

    Name: Floyd


Fullname
  Robert Joseph Newton
Are the attributes and values of this person.


Howdy, podner! E:\d_drive\perlStuff>

Joseph




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

Reply via email to