473,395 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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> lin
Feb 4 '14 #1

✓ answered by miller

<STDIN> is always going to return you a single value. Hashes take key value pairs, so you'd need at least 2 values to initialize your hash. You could do this properly in one of two ways, you could either get the key value pairs in separate input statements, or you could parse the single input and separate the values. Given that you're new, I'd suggest doing the former:

Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my %hash;
  7.  
  8. while (1) {
  9.     print "Enter the hash key (nothing to end): ";
  10.     my $key = <STDIN>;
  11.     chomp($key);
  12.     last if $key eq '';
  13.  
  14.     print "Enter the hash value matched with $key: ";
  15.     my $val = <STDIN>;
  16.     chomp($val);
  17.  
  18.     $hash{$key} = $val;
  19. }
  20.  
  21. print Dumper(\%hash);
  22.  
- Miller

1 2026
miller
1,089 Expert 1GB
<STDIN> is always going to return you a single value. Hashes take key value pairs, so you'd need at least 2 values to initialize your hash. You could do this properly in one of two ways, you could either get the key value pairs in separate input statements, or you could parse the single input and separate the values. Given that you're new, I'd suggest doing the former:

Expand|Select|Wrap|Line Numbers
  1. use Data::Dumper;
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my %hash;
  7.  
  8. while (1) {
  9.     print "Enter the hash key (nothing to end): ";
  10.     my $key = <STDIN>;
  11.     chomp($key);
  12.     last if $key eq '';
  13.  
  14.     print "Enter the hash value matched with $key: ";
  15.     my $val = <STDIN>;
  16.     chomp($val);
  17.  
  18.     $hash{$key} = $val;
  19. }
  20.  
  21. print Dumper(\%hash);
  22.  
- Miller
Feb 9 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jerry Lanphear | last post by:
I am developing an application which allows users to create transactions using excel and uploading the transactions using a .csv text file. I am using System.Security.Cryptography.SHA512 to create...
6
by: spooky | last post by:
hii guyzz Im doing a project which requires that i have a drop down for the country and i require to populate another drop down for state.
1
by: Wayne Deleersnyder | last post by:
Hi All, I was going to write and ask if someone could help me fix the formatting of my output for hash values, but I believe I got it right now. But, because I couldn't find any website or...
9
by: pnsreee | last post by:
Hi All, Please help me to convert csv file to a hash table with out using Text::CSV_XS , Text::CSV. The CSV file is a dynamic file. the fields are like this Numb,Name,Class,Type,......
2
by: padmaja3 | last post by:
Hi all, I have written a query like this. ---------------------------------------------------------- $dbh= DBI->connect("dbi:mysql:dbname=$database;host=$hostname", "$username", "$password"); ...
2
by: padmaja3 | last post by:
Hi all, I have written a query like this. ---------------------------------------------------------- $dbh= DBI->connect("dbi:mysql:dbname=$database;host=$hostname", "$username", "$password"); ...
1
by: bhavanirayala | last post by:
Hi, How can I get the hash values without loop? i.e I want to assign the value of a first key to one variable based on the condition.If that condition falis i need to assign the second value...
4
by: Kerem Gümrükcü | last post by:
Hi, this is not a pure MFC/VC++ question but my apologizes at first. Well, i have a application that calculates the hash for a file. You can request a CALG_SHA1 or a CALG_MD5 for the File. The...
1
by: ramki067 | last post by:
Hi, I've the below HTML code inside a perl file where i'm sending a mail. The mail part and html part is fine. but when i try to add perl code inside DATA as $BStatus and $AStatus its showing as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.