Solved

Copy hash to multi-dimensional array

Posted on 2014-04-14
2
237 Views
Last Modified: 2014-04-14
Hello,

Could someone tell me how I would copy a hash (including the keys) to a mulch-dimensional array?

I have this as my hash, stored in %pids:

$VAR1 = {
          '743' => [
                     '00:1'
                   ],
          '20687' => [
                       '01:3'
                     ],
          '27186' => [
                       '15:3'
                     ],
          '6929' => [
                      '12:0'
                    ],
          '24771' => [
                       '09:0'
                     ],
          '11804' => [
                       '01:1'
                     ],
          '27683' => [
                       '08:3'
                     ],
          '14976' => [
                       '04:3'
                     ],
}

Open in new window


I'm using the following to copy the hash to the array but is keep giving me: "Can't use string ("743") as an ARRAY ref while "strict refs""

my @array = keys %pids;
my $counter = 0;
foreach $var(keys %pids){
	$counter = 0;
	
	while (@array){
		if ($array[$counter] =~ $var){
			$array[$counter][0] = @{$pids{$var}};
			$counter++;
		}
	}
}	
			
print Dumper \@array;

Open in new window


Thanks.
0
Comment
Question by:dloszewski
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39999694
I'm as confused about what you are trying to do as perl is.
What would you want the resulting array to look like?
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39999778
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my $VAR1 = {
          '743' => [
                     '00:1'
                   ],
          '20687' => [
                       '01:3'
                     ],
          '27186' => [
                       '15:3'
                     ],
          '6929' => [
                      '12:0'
                    ],
          '24771' => [
                       '09:0'
                     ],
          '11804' => [
                       '01:1'
                     ],
          '27683' => [
                       '08:3'
                     ],
          '14976' => [
                       '04:3'
                     ],
};
my %pids=%{$VAR1};
my @array;
# I'm guessing you're wanting something like one of these
my($k,$v);
push @array,[$k,@$v] while ($k,$v)=each %pids;                                  
print Dumper \@array;
@array=map{[$_,$pids{$_}]} keys %pids;
print Dumper \@array;
@array=map{[$_,[@{$pids{$_}}]]} keys %pids;
print Dumper \@array;
@array=map{[$_,@{$pids{$_}}]} keys %pids;
print Dumper \@array;
#some of them may look similar, but differ in whether a later modification to the array can also modify the hash and vice versa
__END__
my @array = keys %pids;
my $counter = 0;
foreach $var(keys %pids){
      $counter = 0;
      
      while (@array){
            if ($array[$counter] =~ $var){
                  $array[$counter][0] = @{$pids{$var}};
                  $counter++;
            }
      }
}      
                  
print Dumper \@array;
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question