in reply to Re: dynamically creating the name of a hash key-name
in thread dynamically creating the name of a hash key-name
But don't use strict or -w here! Strict would prevent using a string as a hashref ($$x{key} = "value$i") and -w would warn us that %hash7 was used only once...#!/usr/bin/perl my ($i, @hasharray, $x); # create an array of hashnames (hash0..hash9) for ($i=0; $i<10; $i++) { push @hasharray, "hash$i"; } $i = 10; foreach (@hasharray) { $x = $_; #assign key "key" and value "value$i" to %hashx $$x{key} = "value$i"; $i++; } print "$hash7{key}\n"; #would print "value17"
|
|---|