CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to take hash values from terminal in perl

 



rammohan
Novice

Feb 4, 2014, 12:54 AM

Post #1 of 2 (328 views)
How to take hash values from terminal in perl Can't Post

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.

Code


Code



      
    


FishMonger
Veteran / Moderator

Feb 4, 2014, 6:28 AM

Post #2 of 2 (309 views)
Re: [rammohan] How to take hash values from terminal in perl [In reply to] Can't Post

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.

Code
my $string = <STDIN>; 
chomp $string;
my @elements = split $string;
die "Odd number of elements" if @elements % 2; # sanity check
my %input = @elements;


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives