Re: dumb array/hash question

2003-04-04 Thread R. Joseph Newton
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\perlStuffperson_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]



Re: dumb array/hash question

2003-04-01 Thread Aim
Hi,

Here is one way to build a hash:

my %data = (
12am = 251,
1am   = 102   );
etc.

Regards.

#
jdavis wrote:

 hello,
  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

 Thanks,
 --
 jd
 [EMAIL PROTECTED]

 Bad spellers of the world untie!

 I can't tell if I have worked all my life or
 if I have never worked a single day of my life
 Miguel de Icaza

 --
 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]



Re: dumb array/hash question

2003-04-01 Thread jdavis

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];

Though Im not quite sure why i need to escape the @?

but it works :)

thanks,
jd

On Tue, 2003-04-01 at 03:19, Aim wrote:
 Hi,
 
 Here is one way to build a hash:
 
 my %data = (
 12am = 251,
 1am   = 102   );
 etc.
 
 Regards.
 
 #
 jdavis wrote:
 
  hello,
   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
 
  Thanks,
  --
  jd
  [EMAIL PROTECTED]
 
  Bad spellers of the world untie!
 
  I can't tell if I have worked all my life or
  if I have never worked a single day of my life
  Miguel de Icaza
 
  --
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]
-- 
jd
[EMAIL PROTECTED]

Bad spellers of the world untie!

I can't tell if I have worked all my life or 
if I have never worked a single day of my life
Miguel de Icaza









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



Re: dumb array/hash question

2003-04-01 Thread Ramprasad
Jdavis wrote:
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];
Though Im not quite sure why i need to escape the @?
You are not escaping @ here , you are taking the reference to the object

If you come from the c/c++  world you might understand reference like 
the  operator in scanf(%d,intvar);




but it works :)

thanks,
jd
On Tue, 2003-04-01 at 03:19, Aim wrote:

Hi,

Here is one way to build a hash:

my %data = (
   12am = 251,
   1am   = 102   );
etc.
Regards.

#
jdavis wrote:

hello,
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

Thanks,
--
jd
[EMAIL PROTECTED]
Bad spellers of the world untie!

I can't tell if I have worked all my life or
if I have never worked a single day of my life
   Miguel de Icaza
--
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]


Re: dumb array/hash question

2003-04-01 Thread Scott R. Godin
Jdavis wrote:

 
 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];
 
 Though Im not quite sure why i need to escape the @?
 
 but it works :)

Well, you aren't 'escaping' the @ in the array name. 

You're taking a reference to the original array itself 

if you print $data[0] you'll see that it's an array reference, and needs to 
be accessed via @{ $data[0] }.

I'm not sure this is the best way of doing this, in this case... THe only 
reason I can think of to use arrays in this manner is because you need the 
data in a particular order. 

What's wrong with using the hash itself to store the data? Maybe I need to 
back up and look at the original post again. *headscratching*


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



Re: dumb array/hash question

2003-04-01 Thread Rob Dixon
Jdavis wrote:
 hello,
  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

Hi 'J'. ( It'd be nice to know a name! )

I think the following does what you want. I've written some code to buil
a %data hash as I think you have it from the contents of your post. The final
'while' loop is the bit you're interested in: it takes the hash and builds the
@data array from it as you've described. I've dumped the hash and the array
so that you can see what's in them.

Cheers,

Rob

use strict;
use warnings;

use Data::Dumper;

#   Build the array from the supplied data
#
my @data;
while ( DATA ) {
next unless /\[/;
s/#.+$//;
push @data, [/\w+/g];
}
die unless @data == 2;
die unless @{$data[0]} == @{$data[1]};

#   Build the 'source' hash as I imagine it to be
#
my %data;
@[EMAIL PROTECTED] = @{$data[1]};

#   Empty the @data array and rebuild it from the hash
#
@data = ();
while (my ($k, $v) = each %data) {
push @{$data[0]}, $k;
push @{$data[1]}, $v;
}

#   Show what we've done
#
print Data::Dumper-Dump([\%data, [EMAIL PROTECTED], [qw(*data *data)]);

__END__

@data = (
[12am,1am,2am,3am,4am,5am,6am, 7am, 8am],  ### key
[   251,   102,  55,   36,   113,  200,32,99,   4],  ###val
);

output

%data = (
  '3am' = '36',
  '12am' = '251',
  '4am' = '113',
  '5am' = '200',
  '1am' = '102',
  '6am' = '32',
  '7am' = '99',
  '2am' = '55',
  '8am' = '4'
);
@data = (
  [
'3am',
'12am',
'4am',
'5am',
'1am',
'6am',
'7am',
'2am',
'8am'
  ],
  [
'36',
'251',
'113',
'200',
'102',
'32',
'99',
'55',
'4'
  ]
);




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



Re: dumb array/hash question

2003-04-01 Thread R. Joseph Newton
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.

perldoc perlref
perldoc perlreftut

Joseph


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



dumb array/hash question

2003-03-31 Thread jdavis
hello,
 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

Thanks,
-- 
jd
[EMAIL PROTECTED]

Bad spellers of the world untie!

I can't tell if I have worked all my life or 
if I have never worked a single day of my life
Miguel de Icaza


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



Re: dumb array/hash question

2003-03-31 Thread Stefan Lidman
 hello,
Hi,

  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
 );
 
maybe this is what you want:

my @data;
@{ $data[0]} = sort keys %hash;
@{ $data[1] } = @hash{ @{ $data[0] } }; 

/Stefan

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