updating the hash using a slice:%hash = ( Name => 'myname', Number => 'my1234'. Rank => 'myrank', });
or@hash{"Name", "Number", "Rank"} = ("Kakarot", "3", "SSJ3");
i'd like to use the slice to add data into a hash of a hash (and depending on the data, a further hash from there onwards). Say, this hash:my @desc = ("Name", "Number", "Rank"); my @vegeta = ("Vegeta", "2", "SSJ2"); @hash{@desc} = @vegeta;
This give me an error in printing, but i'm not sure i'm inserting it right?my %h = ( Flinstones => { Dad => "Fred", Mom => "Wilma", Kid => "Pebbles", Pet => "Dino" }, Simpsons => { Dad => "Homer", Mom => "Marge", Kid => "Bart", Pet => "Santas_Little_helper" }); my @desc = ("Dad", "Mom", "Kid", "Pet"); @h{TEST =>{@desc}} = ("Mr", "Mrs", "baby", "none"); foreach my $a (keys %h){ print "Key 1 => $a\n"; foreach my $b (keys %{$h{$a}}){ print "\tKey 2 => $b\n"; print "\t\tValue => $h{$a}{$b}\n"; } }
I also tired the referenced approach:(#prints existing data fine but...) Key 1 => TEST Can't use string ("Mr") as a HASH ref while "strict refs" in use at sl +ice_hoh2.pl line
but this has referenced data showing in the output. Not quite sure how it holds up in the hash :smy $href = { Flinstones => { Dad => "Fred", Mom => "Wilma", Kid => "Pebbles", Pet => "Dino" }, Simpsons => { Dad => "Homer", Mom => "Marge", Kid => "Bart", Pet => "Santas_Little_helper" }}; my $desc = ["Dad", "Mom", "Kid", "Pet"]; @{$href->{TEST}->{[@$desc]}} = ("Mr", "Mrs", "baby", "none");
Where am i going wrong here?, i can get it done in multiple lines:Key 1 => TEST Can't use string ("Mr") as a HASH ref while "strict refs" in use at sl +ice_hoh2.pl line 35.
I'd much rather have slicker looking code :)$h{TEST}{Dad} = "dad_TEST"; $h{TEST}{Mom} = "mon_TEST"; $h{TEST}{Kid} = "Kid_TEST"; $h{TEST}{Pet} = "pet_TEST";
Family1 Dad1 Mom1 Kid1 Pet1 Family2 Dad2 Mom2 Kid2 Pet2
In reply to Slice of Hash of Hash help please by Snk1977
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |