Solved

Copy hash to multi-dimensional array

Posted on 2014-04-14
2
238 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

705 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