![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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! |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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.
|
|
#3
|
|||
|
|||
|
okay, gotcha, but how do i get it to add a user's input to a hash?
|
|
#4
|
|||
|
|||
|
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');
|
|
#5
|
|||
|
|||
|
That still doesn't answer my question!
|
|
#6
|
|||
|
|||
|
It will if you read the link he posted for you.
|
|
#7
|
|||
|
|||
|
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? |
|||
| Google The UNIX and Linux Forums |