get Hash of Array

get Hash of Array

am 25.09.2007 21:47:35 von ebm

is there an easier way to capture the values from a hash of an array?
here's what %data looks like if I use Dumper
---------------------------------------------------
%data using Dumper
$VAR1 = bless( {
'fnames' => [
'OwnerID',
'FaxDateTime',
'ElapsedTime',
'Pages'
],
'field' => {
'ElapsedTime' => 2,
'OwnerID' => 0,
'Pages' => 3,
'FaxDateTime' => 1
}
}
);

------------------------------------------------------------ ---

@keys = keys(%{$data->{'field'}});
@val = values (%{$data->{'field'}});

while(@keys){
$num = pop(@val);
$keyname = pop(@keys);
@k[$num]= $keyname;
}
This way the Key and the Value will be in the correct position of the
array. IE if Data->Field->ElapsedTime = array position 2.
Please let me know.

Thanks in advance.

Re: get Hash of Array

am 26.09.2007 00:34:10 von Jim Gibson

In article <1190749655.935172.183780@g4g2000hsf.googlegroups.com>, ebm
wrote:

> is there an easier way to capture the values from a hash of an array?
> here's what %data looks like if I use Dumper
> ---------------------------------------------------
> %data using Dumper
> $VAR1 = bless( {
> 'fnames' => [
> 'OwnerID',
> 'FaxDateTime',
> 'ElapsedTime',
> 'Pages'
> ],
> 'field' => {
> 'ElapsedTime' => 2,
> 'OwnerID' => 0,
> 'Pages' => 3,
> 'FaxDateTime' => 1
> }
> }
> );
>
> ------------------------------------------------------------ ---
>
> @keys = keys(%{$data->{'field'}});
> @val = values (%{$data->{'field'}});
>
> while(@keys){
> $num = pop(@val);
> $keyname = pop(@keys);
> @k[$num]= $keyname;

If you put 'use strict;' at the beginning of your program, Perl will
tell why this line is in error.

> }
> This way the Key and the Value will be in the correct position of the
> array. IE if Data->Field->ElapsedTime = array position 2.
> Please let me know.

It is not clear from your description what you are trying to do or why.

Here is some simpler code that does the same thing (puts the keys of
the $data->{field} hash into the array k according to the numerical
values in the hash:

foreach my $key ( keys %{$data->{'field'}} ) {
my $num = $data->{'field'}->{$key};
$k[$num]= $key;
}

or simpler:

while( my($key,$num) = each %{$data->{'field'}} ) {
$k[$num]= $key;
}

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Re: get Hash of Array

am 26.09.2007 01:22:14 von Ben Morrow

Quoth ebm :
>
> @keys = keys(%{$data->{'field'}});
> @val = values (%{$data->{'field'}});
>
> while(@keys){
> $num = pop(@val);
> $keyname = pop(@keys);
> @k[$num]= $keyname;
> }

my @k;
my $field = $data->{field};
@k[values %$field] = keys %$field;

Ben