Solved

Copy hash to multi-dimensional array

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Using Grep to Find a file 8 72
scoreUp challenge 14 48
Changing Audit Policies through scripting 5 45
word0 challenge 4 52
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now