Following on from a similar question I asked (Change first key of multi-dimensional Hash in perl) I have a multi-dimensional hash in perl and would like to change MULTIPLE first keys for a chosen value. For example, I have the hash
my %Hash1;
$Hash1{1}{12}=1;
$Hash1{1}{10}=1;
$Hash1{2}{31}=1;
$Hash1{3}{52}=1;
$Hash1{3}{58}=1;
$Hash1{4}{82}=1;
$Hash1{4}{154}=1;
Now I want to replace the values 3 and 4 in the first key with the value 300. After this I would get:
$Hash1{1}{12}=1;
$Hash1{1}{10}=1;
$Hash1{2}{31}=1;
$Hash1{300}{52}=1;
$Hash1{300}{58}=1;
$Hash1{300}{82}=1;
$Hash1{300}{154}=1;
I know I could create a new hash by scanning the original hash and doing the following:
my %Hash2;
foreach my $key1 (sort keys %Hash1) {
foreach my $key2 (keys %{ $Hash1{$key1} }) {
if($key1==3 || $key1==4){
$Hash2{300}{$key2}=1;
} else {
$Hash2{$key1}{$key2}=1;
}
}
}
But is there a quicker way?
$Hash1{300} = {%{$Hash1{3}},%{$Hash1{4}}};
delete $Hash1{3};
delete $Hash1{4};
If you need to replace too many keys, the following function may help.
Use it like replace_first_keys( \%Hash1, [ 3, 4 ], 300 );. The three parameters are reference of hash to modify, reference to array with keys to replace, and the replacement key.
use List::Util;
# REPLACE FIRST KEYS OF $hash LISTED IN #$replace WITH THE KEY $replacement
sub replace_first_keys {
my ( $hash, $replace, $replacement ) = #_;
unshift #$replace, $replacement if exists $hash->{$replacement};
$hash->{$replacement} = {
map { %{ delete $hash->{$_} } }
grep { ( exists $hash->{$_} ) && ( ref $hash->{$_} eq 'HASH' ) }
( List::Util::uniq #$replace )
};
$hash;
}
It also tries to handle the following situations sensibly:
replace_first_keys( \%Hash1, [ 3, 4 ], 2 ); (replacement key exists, older values are overwritten on conflict)
replace_first_keys( \%Hash1, [ 3, 4 ], 4 ); (replacement also present in replace list)
Use this script if you wish to test this.
Related
This question already has answers here:
In Perl, how can I find the index of a given value in an array?
(7 answers)
Closed 9 years ago.
my %hash1 = (
a => ["turkey, mexico"],
b => ["india, china"],
c => ["england, vietnam"],
d => ["usa"],
);
I want to obtain the key related to mexico.
How can I get that?
Tried :
print #($a{$hash1{"mexico"}})
Maybe if you either iterate through the hash keys and return the array containing it, or if you create another hash. For the second, it might look like this:
my %newhash;
for my $key (keys %hash1) {
my #list = split /, / => $hash1{$key}[0];
# or perhaps: my #list = map split(/, /, $_), #{ $hash1{$key} };
for (#list) {
$newhash{$_} = $key;
}
}
$newhash{mexico} eq 'a'; #true
This isn't terribly efficient, but it will work.
print grep { $hash1{$_}[0] =~ /mexico/ } keys %hash1;
and in the case that hash values are arrays with multiple elements,
print grep { grep { $_ eq "mexico" } #{$hash1{$_}} } keys %hash1;
I'm building %grouped from %uniq_c, where %grouped contains the key/value pairs generated by splitting %uniq_c's key IF %uniq_c's value is high enough. It's fairly efficient, but I'd like to do better.
%uniq_c = (
'foo:baz' => 3,
'foo:quux' => 12,
'bar:corge' => 15,
'bar:fred' => 8,
);
foreach my $gv (keys %uniq_c) {
if( $uniq_c{$gv} >= 10 ) {
my ($g, $v) = split /:/, $gv, 2;
push( #{$grouped{$g}}, $v );
}
}
I think there are three string copies happening per loop iteration; 1 for $g and 2 for $v. Is there a way to eliminate one of the $v copies, or better yet, a $v and a $g copy (some sort of string slicing perhaps)?
For reference, Data::Dump::dump(%grouped) produces the following:
(
"bar", ["corge"],
"foo", ["quux"],
)
The copying of the values returned by split is very efficient; the string buffer is stolen instead of copied. But there is another copy done when you push $v. All three of these copies can be avoided through aliasing.
use Data::Alias qw( alias );
foreach my $gv (keys %uniq_c) {
if( $uniq_c{$gv} >= 10 ) {
alias my ($g, $v) = split /:/, $gv, 2;
alias push #{$grouped{$g}}, $v;
}
}
I would like to make the value the key, and the key the value. What is the best way to go about doing this?
Adapted from http://www.dreamincode.net/forums/topic/46400-swap-hash-values/:
Assuming your hash is stored in $hash:
while (($key, $value) = each %hash) {
$hash2{$value}=$key;
}
%hash=%hash2;
Seems like much more elegant solution can be achieved with reverse (http://www.misc-perl-info.com/perl-hashes.html#reverseph):
%nhash = reverse %hash;
Note that with reverse, duplicate values will be overwritten.
Use reverse:
use Data::Dumper;
my %hash = ('month', 'may', 'year', '2011');
print Dumper \%hash;
%hash = reverse %hash;
print Dumper \%hash;
As mentioned, the simplest is
my %inverse = reverse %original;
It "fails" if multiple elements have the same value. You could create an HoA to handle that situation.
my %inverse;
push #{ $inverse{ $original{$_} } }, $_ for keys %original;
So you want reverse keys & vals in a hash? So use reverse... ;)
%hash2 = reverse %hash;
reverting (k1 => v1, k2 => v2) - yield (v2=>k2, v1=>k1) - and that is what you want. ;)
my %orig_hash = (...);
my %new_hash;
%new_hash = map { $orig_hash{$_} => $_ } keys(%orig_hash);
The map-over-keys solution is more flexible. What if your value is not a simple value?
my %forward;
my %reverse;
#forward is built such that each key maps to a value that is a hash ref:
#{ a => 'something', b=> 'something else'}
%reverse = map { join(',', #{$_}{qw(a b)}) => $_ } keys %forward;
Here is a way to do it using Hash::MultiValue.
use experimental qw(postderef);
sub invert {
use Hash::MultiValue;
my $mvh = Hash::MultiValue->from_mixed(shift);
my $inverted;
$mvh->each( sub { push $inverted->{ $_[1] }->#* , $_[0] } ) ;
return $inverted;
}
To test this we can try the following:
my %test_hash = (
q => [qw/1 2 3 4/],
w => [qw/4 6 5 7/],
e => ["8"],
r => ["9"],
t => ["10"],
y => ["11"],
);
my $wow = invert(\%test_hash);
my $wow2 = invert($wow);
use DDP;
print "\n \%test_hash:\n\n" ;
p %test_hash;
print "\n \%test_hash inverted as:\n\n" ;
p $wow ;
# We need to sort the contents of the multi-value array reference
# for the is_deeply() comparison:
map {
$test_hash{$_} = [ sort { $a cmp $b || $a <=> $b } #{ $test_hash{$_} } ]
} keys %test_hash ;
map {
$wow2->{$_} = [ sort { $a cmp $b || $a <=> $b } #{ $wow2->{$_} } ]
} keys %$wow2 ;
use Test::More ;
is_deeply(\%test_hash, $wow2, "double inverted hash == original");
done_testing;
Addendum
Note that in order to pass the gimmicky test here, the invert() function relies on %test_hash having array references as values. To work around this if your hash values are not array references, you can "coerce" the regular/mixed hash into a multi-value hash thatHash::MultiValue can then bless into an object. However, this approach means even single values will appear as array references:
for ( keys %test_hash ) {
if ( ref $test_hash{$_} ne 'ARRAY' ) {
$test_hash{$_} = [ $test_hash{$_} ]
}
}
which is longhand for:
ref($_) or $_ = [ $_ ] for values %test_hash ;
This would only be needed to get the "round trip" test to pass.
Assuming all your values are simple and unique strings, here is one more easy way to do it.
%hash = ( ... );
#newhash{values %hash} = (keys %hash);
This is called a hash slice. Since you're using %newhash to produce a list of keys, you change the % to a #.
Unlike the reverse() method, this will insert the new keys and values in the same order as they were in the original hash. keys and values always return their values in the same order (as does each).
If you need more control over it, like sorting it so that duplicate values get the desired key, use two hash slices.
%hash = ( ... );
#newhash{ #hash{sort keys %hash} } = (sort keys %hash);
What is the best practise to solve this?
if (... )
{
push (#{$hash{'key'}}, #array ) ;
}
else
{
$hash{'key'} ="";
}
Is that bad practise for storing one element is array or one is just double quote in hash?
I'm not sure I understand your question, but I'll answer it literally as asked for now...
my #array = (1, 2, 3, 4);
my $arrayRef = \#array; # alternatively: my $arrayRef = [1, 2, 3, 4];
my %hash;
$hash{'key'} = $arrayRef; # or again: $hash{'key'} = [1, 2, 3, 4]; or $hash{'key'} = \#array;
The crux of the problem is that arrays or hashes take scalar values... so you need to take a reference to your array or hash and use that as the value.
See perlref and perlreftut for more information.
EDIT: Yes, you can add empty strings as values for some keys and references (to arrays or hashes, or even scalars, typeglobs/filehandles, or other scalars. Either way) for other keys. They're all still scalars.
You'll want to look at the ref function for figuring out how to disambiguate between the reference types and normal scalars.
It's probably simpler to use explicit array references:
my $arr_ref = \#array;
$hash{'key'} = $arr_ref;
Actually, doing the above and using push result in the same data structure:
my #array = qw/ one two three four five /;
my $arr_ref = \#array;
my %hash;
my %hash2;
$hash{'key'} = $arr_ref;
print Dumper \%hash;
push #{$hash2{'key'}}, #array;
print Dumper \%hash2;
This gives:
$VAR1 = {
'key' => [
'one',
'two',
'three',
'four',
'five'
]
};
$VAR1 = {
'key' => [
'one',
'two',
'three',
'four',
'five'
]
};
Using explicit array references uses fewer characters and is easier to read than the push #{$hash{'key'}}, #array construct, IMO.
Edit: For your else{} block, it's probably less than ideal to assign an empty string. It would be a lot easier to just skip the if-else construct and, later on when you're accessing values in the hash, to do a if( defined( $hash{'key'} ) ) check. That's a lot closer to standard Perl idiom, and you don't waste memory storing empty strings in your hash.
Instead, you'll have to use ref() to find out what kind of data you have in your value, and that is less clear than just doing a defined-ness check.
I'm not sure what your goal is, but there are several things to consider.
First, if you are going to store an array, do you want to store a reference to the original value or a copy of the original values? In either case, I prefer to avoid the dereferencing syntax and take references when I can:
$hash{key} = \#array; # just a reference
use Clone; # or a similar module
$hash{key} = clone( \#array );
Next, do you want to add to the values that exist already, even if it's a single value? If you are going to have array values, I'd make all the values arrays even if you have a single element. Then you don't have to decide what to do and you remove a special case:
$hash{key} = [] unless defined $hash{key};
push #{ $hash{key} }, #values;
That might be your "best practice" answer, which is often the technique that removes as many special cases and extra logic as possible. When I do this sort of thing in a module, I typically have a add_value method that encapsulates this magic where I don't have to see it or type it more than once.
If you already have a non-reference value in the hash key, that's easy to fix too:
if( defined $hash{key} and ! ref $hash{key} ) {
$hash{key} = [ $hash{key} ];
}
If you already have non-array reference values that you want to be in the array, you do something similar. Maybe you want an anonymous hash to be one of the array elements:
if( defined $hash{key} and ref $hash{key} eq ref {} ) {
$hash{key} = [ $hash{key} ];
}
Dealing with the revised notation:
if (... )
{
push (#{$hash{'key'}}, #array);
}
else
{
$hash{'key'} = "";
}
we can immediately tell that you are not following the standard advice that protects novices (and experts!) from their own mistakes. You're using a symbolic reference, which is not a good idea.
use strict;
use warnings;
my %hash = ( key => "value" );
my #array = ( 1, "abc", 2 );
my #value = ( 22, 23, 24 );
push(#{$hash{'key'}}, #array);
foreach my $key (sort keys %hash) { print "$key = $hash{$key}\n"; }
foreach my $value (#array) { print "array $value\n"; }
foreach my $value (#value) { print "value $value\n"; }
This does not run:
Can't use string ("value") as an ARRAY ref while "strict refs" in use at xx.pl line 8.
I'm not sure I can work out what you were trying to achieve. Even if you remove the 'use strict;' warning, the code shown does not detect a change from the push operation.
use warnings;
my %hash = ( key => "value" );
my #array = ( 1, "abc", 2 );
my #value = ( 22, 23, 24 );
push #{$hash{'key'}}, #array;
foreach my $key (sort keys %hash) { print "$key = $hash{$key}\n"; }
foreach my $value (#array) { print "array $value\n"; }
foreach my $value (#value) { print "value $value\n"; }
foreach my $value (#{$hash{'key'}}) { print "h_key $value\n"; }
push #value, #array;
foreach my $key (sort keys %hash) { print "$key = $hash{$key}\n"; }
foreach my $value (#array) { print "array $value\n"; }
foreach my $value (#value) { print "value $value\n"; }
Output:
key = value
array 1
array abc
array 2
value 22
value 23
value 24
h_key 1
h_key abc
h_key 2
key = value
array 1
array abc
array 2
value 22
value 23
value 24
value 1
value abc
value 2
I'm not sure what is going on there.
If your problem is how do you replace a empty string value you had stored before with an array onto which you can push your values, this might be the best way to do it:
if ( ... ) {
my $r = \$hash{ $key }; # $hash{ $key } autoviv-ed
$$r = [] unless ref $$r;
push #$$r, #values;
}
else {
$hash{ $key } = "";
}
I avoid multiple hash look-ups by saving a copy of the auto-vivified slot.
Note the code relies on a scalar or an array being the entire universe of things stored in %hash.
In one of my main( or primary) routines,I have two or more hashes. I want the subroutine foo() to recieve these possibly-multiple hashes as distinct hashes. Right now I have no preference if they go by value, or as references. I am struggling with this for the last many hours and would appreciate help, so that I dont have to leave perl for php! ( I am using mod_perl, or will be)
Right now I have got some answer to my requirement, shown here
From http://forums.gentoo.org/viewtopic-t-803720-start-0.html
# sub: dump the hash values with the keys '1' and '3'
sub dumpvals
{
foreach $h (#_)
{
print "1: $h->{1} 3: $h->{3}\n";
}
}
# initialize an array of anonymous hash references
#arr = ({1,2,3,4}, {1,7,3,8});
# create a new hash and add the reference to the array
$t{1} = 5;
$t{3} = 6;
push #arr, \%t;
# call the sub
dumpvals(#arr);
I only want to extend it so that in dumpvals I could do something like this:
foreach my %k ( keys #_[0]) {
# use $k and #_[0], and others
}
The syntax is wrong, but I suppose you can tell that I am trying to get the keys of the first hash ( hash1 or h1), and iterate over them.
How to do it in the latter code snippet above?
I believe this is what you're looking for:
sub dumpvals {
foreach my $key (keys %{$_[0]}) {
print "$key: $_[0]{$key}\n";
}
}
An element of the argument array is a scalar, so you access it as $_[0] not #_[0].
keys operates on hashes, not hash refs, so you need to dereference, using %
And of course, the keys are scalars, not hashes, so you use my $key, not my %key.
To have dumpvals dump the contents of all hashes passed to it, use
sub dumpvals {
foreach my $h (#_) {
foreach my $k (keys %$h) {
print "$k: $h->{$k}\n";
}
}
}
Its output when called as in your question is
1: 2
3: 4
1: 7
3: 8
1: 5
3: 6