in reply to Hash: Removing a specific value from a multi-valued key

I agree with Bloodnok about the data structure. You have a Hash of Array. Basically this is a hash key that points to not one value, but actually a reference to a list of values.

The q/11.5/ syntax is a type of a Perl quote and it just means '11.5'. There are a bunch of these short-cut quote operators in Perl (q(single quote),qq (double quote), qw (quote word),etc). If you are going to write more Perl, buy "Programming Perl" by Larry Wall, Tom Christiansen and Jon Orwant and look at page 63 which has a table about this "quoting" subject.

I prefer the below synatx.

$hashA{q/11.5/} = [ grep { $_ != 1734789 } @{$hashA{q/11.5/}} ];
First, @{$hashA{q/11.5/}}, the q/11.5/ just means '11.5'.
$hashA{'11.5'} points to an anonymous array of values.
@{$hashA{q/11.5/}} expands that anonymous array into a list of values
That list goes into grep which makes a yes/no decision for each list item.
If the value is not 1734789 then the value gets put into yet another anonymous list. Meaning that values = 1734789 are deleted.
The output of the grep{} is a list and the enclosing [ ] brackets around this big expression allocate memory for that list which is then assigned back to the $hashA{'11.5'} key.

See below code.

#!/usr/bin/perl -w use strict; use Data::Dumper; my %hashA = ( '11.5' => [ 1723478, 1734789, 1798761, ], '11.12' => [ 1700123, ], '11.01' => [ 1780345, ] ); print Dumper (\%hashA); $hashA{q/11.5/} = [ grep { $_ != 1734789 } @{$hashA{q/11.5/}} ]; print Dumper (\%hashA); __END__ Prints: $VAR1 = { '11.01' => [ 1780345 ], '11.12' => [ 1700123 ], '11.5' => [ 1723478, 1734789, 1798761 ] }; $VAR1 = { '11.01' => [ 1780345 ], '11.12' => [ 1700123 ], '11.5' => [ 1723478, 1798761 ] };
1734789 was in the anonymous array assigned to $hashA{'11.5'} and it is no longer there.

Replies are listed 'Best First'.
Re^2: Hash: Removing a specific value from a multi-valued key
by eshwar (Novice) on Oct 01, 2009 at 10:00 UTC
    Hi,
    Thanks to Bloodnok, toolic and Marshall for your inputs. You ppl are amazing.
    I will try what you have suggested.

    Thanks,
    Eshwar