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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -