How can I Initialise and clear multiple hash in one line.
Ex:
my %hash1 = ();
my %hash2 = ();
my %hash3 = ();
my %hash4 = ();
to
my ( %hash1, %hash2, %hash3, %hash4 ) = ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It appears (from your comments) that you really want to empty hashes that already have stuff in them. You can do it like this:
Complete example:
A variable declaration always gives you an empty variable, so there is no need to set it to empty. This is true even in a loop:
This will print
1over and over; even though you might expect$xto retain its value, it does not.Explanation: Perl allows list assignment like
($foo,$bar) = (1,2). If the list on the right is shorter, any remaining elements get assignedundef. Thus assigning the empty list to a list of variables makes them all undefined.Another useful way to set a bunch of things is the
xoperator:This sets all three variables to 100. It doesn’t work so well for hashes, though, because each one needs a list assigned to it.