Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Hash user input

by Nansh (Acolyte)
on May 24, 2017 at 17:14 UTC ( [id://1191115]=perlquestion: print w/replies, xml ) Need Help??

Nansh has asked for the wisdom of the Perl Monks concerning the following question:

Hello I have two hash say %hash1 and %hash2

%hash1 and %hash2 these hash contains some keys and values

I need to give these hash as a user input

my %hash_input=@_; my %hash_input2=@_;
can i take two hash as a user input like i mentioned above??? Thanks in Advance.

Replies are listed 'Best First'.
Re: Hash user input
by stevieb (Canon) on May 24, 2017 at 17:24 UTC

    Are you talking about subroutine parameters, or input from the command line? If it's a subroutine, you send in references to the hashes. All parameters sent into a subroutine get flattened into one large list, so identifying where one hash starts and another begins is impossible:

    my %hash_1 = (a => 1, b => 2); my %hash_2 = (z => 26, y => 25); foo(\%hash_1, \%hash_2); sub foo { my ($href_1, $href_2) = @_; print "$_: $href_1->{$_}\n" for keys %$href_1; print "$_: $href_2->{$_}\n" for keys %$href_2; }

      Hello stevieb,

      I think you meant to write %$hreef_1 instead of %$href1 same for the second inside the function. ;)

      Seeking for Perl wisdom...on the process of learning...not there...yet!

        Indeed, nice catch! That's what I get for not testing the code, or at least writing in an "untested" note :) Thanks!

      But it is treating both the hash as a single hash.

        Hello Nansh,

        As stevieb and Anonymous Monk proposed some possible solutions to your problem, your answer does not really help.

        What I mean is your main goal, what are you trying to achieve? Do you want to pass two hashes inside a subroutine, you have already a solution on this. You want to pass the STDIN as parameters, you have an answer again.

        So what you really want to do, we are still not able to understand.

        Looking forward to your reply.

        Seeking for Perl wisdom...on the process of learning...not there...yet!

        Hello again

        Why I still believe that your question in not answered?

        Although the rest of the monks they have replied to your question in a variety of ways stating more or less the same I think you are getting confused on how the parameters are passed on the subroutine. Correct if I am wrong but I think you are getting confused about the @_ in the subroutine why there is only one and not two correct?

        Sample of code taken from (perlsub/Pass by Reference) with minor modifications:

        #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash_1 = (a => 26, b => 2, c => 3); my %hash_2 = (z => 26, y => 25); my ($ahashref, $bhashref) = func(\%hash_1, \%hash_2); print Dumper $ahashref, $bhashref; sub func { my ($cref, $dref) = @_; if (keys %$cref > keys %$dref) { return ($cref, $dref); } else { return ($dref, $cref); } }

        I use (Data::Dumper) for debugging purposes to analyze the @_. Sample of the output from the example provided above:

        print Dumper \@_; $VAR1 = [ { 'b' => 2, 'a' => 26, 'c' => 3 }, { 'y' => 25, 'z' => 26 } ];

        What you see here is an perldsc/ARRAYS OF HASHES. Maybe the perlsub/DESCRIPTION will help a bit more:

        Any arguments passed in show up in the array @_ . (They may also show up in lexical variables introduced by a signature; see Signatures below.) Therefore, if you called a function with two arguments, those would be stored in $_[0] and $_1 . The array @_ is a local array, but its elements are aliases for the actual scalar parameters. In particular, if an element $_[0] is updated, the corresponding argument is updated (or an error occurs if it is not updatable). If an argument is an array or hash element which did not exist when the function was called, that element is created only when (and if) it is modified or a reference to it is taken. (Some earlier versions of Perl created the element whether or not the element was assigned to.) Assigning to the whole array @_ removes that aliasing, and does not update any arguments.

        If still your question is not answered let us know.

        Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Hash user input
by BillKSmith (Monsignor) on May 24, 2017 at 22:36 UTC
    I assume that you mean you want to pass two hashes to a subroutine. You have already been told a few times that this can only be done by passing references. Although it is generally considered bad practice to do so, this fact can be hidden from the calling program by the use of prototypes. Note that the real arguments are converted to references. The subroutine must treat them as such.
    use strict; use warnings; use Data::Dumper; sub some_sub (\%\%) { my ($hash_ref1, $hash_ref2) = @_; my %sub_hash1 = %$hash_ref1; my %sub_hash2 = %$hash_ref2; print Dumper( \%sub_hash1, \%sub_hash2 ); } do { my %hash1 = (key_a => 'value_a', key_b =>'value_b'); my %hash2 = (key_c => 'value_c', key_d =>'value_d'); some_sub( %hash1, %hash2 ); # Args appear to be hashes. } OUTPUT: $VAR1 = { 'key_b' => 'value_b', 'key_a' => 'value_a' }; $VAR2 = { 'key_d' => 'value_d', 'key_c' => 'value_c' };
Re: Hash user input
by Anonymous Monk on May 24, 2017 at 17:18 UTC
    %hash1 and %hash2
    a Nansh
    a Thanks in Advance

    (see Getopt::Long)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1191115]
Approved by Corion
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-05-26 18:36 GMT
Find Nodes?
    Voting Booth?

    No recent polls found