Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Thanked 0 Times in 0 Posts

    How to take hash values from terminal in perl

    Hi to all ! I'm new to this forum an as well as Perl scripting . My question is how to take Hash values from user using terminal in Perl.

    my code is :
    my%inputline = <STDIN>;
    print %inputline;

    it showing some error
    Odd number of elements in hash assignment at until.pl line 199, <STDIN> line 1.
    ram 47 nasdfi 47 klsdjf 258 dshafa 639
    Use of uninitialized value $inputline{"ram 47 nasdfi 47 klsdjf 258 dsha"...} in print at until.pl line 202, <STDIN> line 1.

  2. #2
    Super Moderator
    Join Date
    May 2005
    Southern tip of Silicon Valley
    Thanked 233 Times in 226 Posts
    Cross posted on perlguru

    ram 47 nasdfi 47 klsdjf 258 dshafa 639 is being read-in as a single string which you then use as a key in the hash. Hashes are key/value pairs so they need an even number of elements.

    You need to split that single string into its 8 elements then use those elements to assign your hash.

    This can be done in 1 line, but I'll break it up for clarity.
    my $string = <STDIN>;
    chomp $string;
    my @elements = split $string;
    die "Odd number of elements" if @elements % 2;  # sanity check
    my %input = @elements;

  3. Users who have thanked FishMonger for this post:

    Rammohan (02-11-2014)


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts