Solved

Copy hash to multi-dimensional array

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 116
bash while loop reading input from data section in script 7 71
Control Number of Log Files -Perl 7 88
Help With Shell Script Centos 6 6 70
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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 …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

820 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