The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Hash Question in Perl deadletter Shell Programming and Scripting 6 07-17-2008 02:07 PM
Perl Hash Harikrishna Shell Programming and Scripting 1 06-04-2008 04:03 AM
Perl Hash Harikrishna Shell Programming and Scripting 1 06-02-2008 08:45 PM
Hash in perl Harikrishna Shell Programming and Scripting 1 06-02-2008 01:00 AM
hash,array and perl new2ss Shell Programming and Scripting 3 05-23-2007 08:30 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-30-2008
Registered User
 

Join Date: Apr 2008
Location: Meridian, MS, USA
Posts: 7
perl adding items to a hash

how would you go about taking a user's input and adding it to a hash?
i'd also like it to permanently add the input to the hash. not just in the ram, add it into the script!
Reply With Quote
Forum Sponsor
  #2  
Old 04-30-2008
Registered User
 

Join Date: Apr 2008
Location: European Union/Germany
Posts: 183
To mix up data and code is really bad style. Even worse is to alter a script by itsself to add data to it. Better and easier would be to write that hash to some data file at script exit and read it from there at script start.
Reply With Quote
  #3  
Old 04-30-2008
Registered User
 

Join Date: Apr 2008
Location: Meridian, MS, USA
Posts: 7
okay, gotcha, but how do i get it to add a user's input to a hash?
Reply With Quote
  #4  
Old 04-30-2008
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,477
Perl has a built-in Storable module that you can use to store complex data structures in some sort of storage so that you can recover later.

This example is from the Storable man page with a few comments added by me:

Code:
use Storable;
# %table is the hash to be stored in file 'file'
store \%table, 'file';
# Then recover the hash from file (you need to de-reference it to get its content)
$hashref = retrieve('file');
Storable - perldoc.perl.org
Reply With Quote
  #5  
Old 04-30-2008
Registered User
 

Join Date: Apr 2008
Location: Meridian, MS, USA
Posts: 7
That still doesn't answer my question!
Reply With Quote
  #6  
Old 04-30-2008
Registered User
 

Join Date: Jan 2008
Posts: 327
It will if you read the link he posted for you.
Reply With Quote
  #7  
Old 04-30-2008
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,477
Quote:
Originally Posted by andrew2325 View Post
That still doesn't answer my question!
It does.

I do expect that you at least know what a hash is in Perl and how you add key-value pairs to it and retrieve something from it. So, you were asking how to persist the data to a file so that you can recover it later, and so I suggested Storable demonstrating how to save to and restore from a file using an example. It's all in the docs and I already provided you with the link to the man page in case you are so impatient to locate the man page yourself.

If you don't understand the example, run it and see the results yourself. If you are totally new to Perl, you should first learn the basics of the language yourself and we can never teach Perl here with so limited space.

I think I have already provided you with everything that is relevant as far as I conceive.

So tell me, in what ways can I serve you better?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0