command line interface - How can I elegantly turn user input into a variable name in Perl? -
i want use input user pick hash operate on. currently, have cumbersome code:
my ( %hash1, %hash2 ); print "which hash work (1 or 2)? "; $which = <>; chomp $which; &do_something( %hash1 ) if $which == 1; &do_something( %hash2 ) if $which == 2;
is there more elegant way this? thinking of using %{"hash$which"}
, doesn't seem work. think
$which = "hash" . chomp $which; &do_something( %{"$which"} );
would work, best way it?
thanks help, since first post.
why it's stupid use variable variable name
you want
my @hashes; print "which hash work (1 or 2)? "; $which = <>; chomp $which; do_something( %{ $hashes[$which] } );
just in case don't know, use use strict; use warnings;
.
Comments
Post a Comment