473,388 Members | 1,377 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,388 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 2024
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.