Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

copying array of hashes

Posted on 2009-05-10
6
Medium Priority
?
243 Views
Last Modified: 2012-05-06
how can I make a copy of an array of hashes so that the original one isn't affected? I don't have use storage in my PERL version
0
Comment
Question by:cucugirl
[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
6 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 336 total points
ID: 24349370
@array=({k1=>'v1',k2=>'v2'},{k1=>'v1',k2=>'v2'});
@copy=map{{%$_}}@array;
$copy[1]{k2}++;
print "$copy[1]{k2} $array[1]{k2}\n";
0
 
LVL 39

Expert Comment

by:Adam314
ID: 24351547
>>I don't have use storage
I'm assuming you meant you don't have the Storable module?  If so, can you install it?  Have you tried?  Did you get an error?
0
 
LVL 5

Assisted Solution

by:vikaskhoria
vikaskhoria earned 332 total points
ID: 24352113
Check my script below:
(This deletes elements from the hash and the original hash is modified to the desired hash)

Thanks,
Vikas.

#!/usr/bin/perl
 
my %my_hash = ('TEST' =>'T' ,'QUIZ'=>'4','HWORK'=>'3');
my @table = ( {'TEST'=>'T'}, {'TEST'=>'F'},{'QUIZ'=>'1'},{'QUIZ'=> '2'},{'HWORK'=>'3'}, {'HWORK'=>'3'});
 
for my $value (keys %my_hash)
{
        my $found = 0;
        foreach $i (0 .. @table-1)
        {
                if ( $table[$i]{$value} == $my_hash{$value} ) {
                        $found = 1;
                }
        }
        if ( ! $found )
        {
                delete($my_hash{$value});
        }
}

Open in new window

0
 
LVL 5

Expert Comment

by:vikaskhoria
ID: 24361590
Check this link below:
http://www.perlmonks.org/?node_id=93693

This talks about copying array of hashes.
This may be of some help to you.
0
 
LVL 27

Assisted Solution

by:wilcoxon
wilcoxon earned 332 total points
ID: 25953183
The best way is to use Clone (this handles any level of nested structures).  Alternately, if you know that the hashes only contain scalars, it's easy to copy them manually.
my $orig = [{first=>1,second=>2},{first=>3,second=>4}]; 
# using Clone
use Clone qw(clone);
my $copy = clone($orig); 
# manually copying
## slow/inefficient way
my $copy = [];
foreach my $el (@$orig) {
    my %tmp;
    foreach my $key (%$el) {
        $tmp{$key} = $el->{$key};
    }
    push @$copy, {%tmp};
}
## better way
my $copy = [];
foreach my $el (@$orig) {
    push @$copy, {%$el};
}
## ozo's way - more succinct but should be about same performance (I think)
my $copy = [map { {%$_} } @$orig];

Open in new window

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

636 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