Solved

Copy hash to multi-dimensional array

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Batch File - Combing output in a single line 3 60
Ned Perl Snippet to Read Files in Directory 5 62
VBA Shell can't Find Word document 11 77
Adjust the position 3 50
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Utilizing an array to gracefully append to a list of EmailAddresses
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 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

27 Experts available now in Live!

Get 1:1 Help Now