Link to home
Start Free TrialLog in
Avatar of gmanpert
gmanpert

asked on

Hash of Array slices?

I have a hash of Array as follows:

 keys = var1   var2  var3
            ---------------------
             1       a       john
             2       b       kim
             3       c       tim

such that

$value_ref = $hash{'var1'};
@values = @$value_ref;
print "Values: @values \n";

prints  1 2 3

What I'm trying to figure out is how to get a different hash, ( a simple hash - not a hash of arrays)
which has the same keys and just the values for the second row.   I want to end up with

$hash{'var1'} = 2;
$hash{'var2'} = 'b';
$hash{'var3'} = 'kim';

That is, I'd like to take a slice of the original hash, if there is such a thing?
Obviously, what I've done would work if I assigned a different name to $hash.  But this isn't really what I'm
looking for.  Thus, what I am looking for is the syntax which will let me do this simply and efficiently.

-G


ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gmanpert
gmanpert

ASKER

I guess still don't understand how your code works?  

Using the following code on my hash,

foreach $k (sort keys %hash) {
  print "Skey: $k  Value @{$hash{$k}} \n";
}

prints,
   Skey: var1  Value 1 2 3
   Skey: var2  Value a b c
   Skey: var3  Value john kim tim

Doing,

$_ = $_->[1] for values %hash;
print ".. $_ \n";

Gets me,

  Use of uninitialized value in concatenation ...

Trying,

@$_ = @{$_}[1] for values %hash;
print "... @$_ \n";

Gets me the same thing.
   
I think what I want is something such as:

foreach $k (sort keys %hash) {
  $newhash{$k} = $hash{$k}[1];
}

But, this doesn't seem to work either.

-G



SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, I've gotten a couple of these forms to work but need more help understanding what is actually going on.

$_ = $_ ->[1] for values %hash;

Does what I was after, namely returns a hash with one row of values matched to each key.

Here is what I think I understand... borken into pieces:

 @hash_values = values %hash   # puts values of hash (array references) into array
 for values %hash                       # sets up loop of those values passing them to $_ each time through
                                                 # thus $_ is the reference to the array of values associated with the key
 $_->[1]                                     # dereferences the second value of the array and assigns it to $_

 My question is how does it get reassigned back to the original hash?  It seems to me I should be ending
up with one scalar at the end... but obviously, I don't ?


I could not get this form to work:

@$_ = @{$_}[1] for values %hash;
foreach $k (sort keys %hash) {
  print "Skey: $k  Value @{$hash{$k}} \n";
}

It runs.  But it prints with no values:

Skey: var1  Value
Skey: var2  Value
Skey: var3  Value

Which, brings me to trying to figuring out this form:

  @{$hash{$k}}

So,

   {$k}               # dereferences the hash key
   $hash{$k}       # retrieves the value of the hash for the key $k
  @{$hash{$k}}  # I don't really understand this.  dereferencing hash value?  Assigning it to an array?


-G
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial