perl adding items to a hash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl adding items to a hash
# 8  
Old 05-01-2008
That wasn't what i was asking at all. I understand how to add things to a hash. I just don't understand how to add a user's input to a hash. I get that I need to have it write it to a file. I get THAT, but I don't get how to get it to take a user's input and add it to a hash. I'm getting tired of repeating myself.Smilie
# 9  
Old 05-01-2008
get user input
Code:
$userinput = <>;

# 10  
Old 05-02-2008
yea, i got that part. this is what won't work:
Code:
	print "Enter new variable for hash\n";
	$variable = <>;
	chomp($variable);
	print "Enter what it equals:";
	$newequal = <>;
	chomp($newequal);
	$addtohash{$variable}=$newequal;

it doesn't add it to the hash!
# 11  
Old 05-02-2008
Sure it will. If you have a hash %addtohash your code will add a new key with the value. Of course it will only add it into memory.
# 12  
Old 05-02-2008
Where do I add %addtohash in the code I posted. I tried adding it in a few different places, and it didn't work.Smilie
# 13  
Old 05-02-2008
It is exactly this statement you posted

Quote:
Originally Posted by andrew2325
yea, i got that part. this is what won't work:
Code:
	$addtohash{$variable}=$newequal;

that sets the new key-value to the hash %addtohash.

The key is the value of the variable $variable. That was what you read from the standard input.

The value is the value of the variable $newequal. Again, you read that from the standard input.

That is exactly what your code does, but whether your code posted does exactly what you expected it to do, only you can tell.

I'm tired having to explain basic syntax like this either. Please consider revise the Perl basics. You really need it.
# 14  
Old 05-02-2008
Quote:
Originally Posted by andrew2325
Where do I add %addtohash in the code I posted. I tried adding it in a few different places, and it didn't work.Smilie
Sorry, I'm giving up. Either you can't follow simple instuctions or we can't give them, either way, this is going no where.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

2. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

3. Shell Programming and Scripting

Perl hash help

Hi , i have the below code its working fine when i execute in unix , but its not working in windows could you pls explain me where i am going wrong. This is the program $data = { '1' => 'one' , '2' => 'two' , 3 => 'three' }; print "hello : $data->{'1'}... (2 Replies)
Discussion started by: ragilla
2 Replies

4. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

5. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

6. Shell Programming and Scripting

perl hash

Hi i am reading one file and creating Hash from the contents of it my issue is there are 3 different files in 3 different locations having same structure so for parsing these files i have one subroutine which returns hash after reading all the 3 files i need to create consolidated hash from three... (2 Replies)
Discussion started by: zedex
2 Replies

7. Shell Programming and Scripting

perl hash

This is my data 1 0 1 0 1 1 1 2 1 6 1 7 Assume that first field is key and 2nd field is value I want to create a hash in perl, on this data. My hash should having uniq key and all values by , separated. 1,0,0,1,2,6,7 1 will be my key and rest of are should be values. (3 Replies)
Discussion started by: pritish.sas
3 Replies

8. Shell Programming and Scripting

perl using hash

i want to ask is it i can use hash in perl to store a page number with a list of words which is in that page and then print it out? Example Page 1 contains a are boy cat ............. (a list of sorted words) how can i store it in a hash? Thank you (3 Replies)
Discussion started by: mingming88
3 Replies

9. Shell Programming and Scripting

Perl Hash

hi i have two hash achi %disk1,%disk2 with( key, value) (key1,value1) How to store it in another hash.. Plz replyyy. Regards Hari (1 Reply)
Discussion started by: Harikrishna
1 Replies

10. Shell Programming and Scripting

Hash in perl

Hi Help me with some good links of Hash with in Hash .(Multidimensional hash).. Regards Harikrishna (1 Reply)
Discussion started by: Harikrishna
1 Replies
Login or Register to Ask a Question