Important alert: (current site time 7/15/2013 8:50:37 PM EDT)
 

VB icon

Store your hashtable or array in a file a recover it without any processing

Email
Submitted on: 7/19/2003 5:08:34 PM
By: Bruno Martins 
Level: Beginner
User Rating: Unrated
Compatibility: 5.0 (all versions), Active Perl specific, 4.0 (all versions), 3.0 (all versions), Pre 3.0
Views: 11306
(About the author)
 
     This code is an example of use for the Data::Dumper module. It is available in all perl versions for any operating system.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
=**************************************
= Name: Store your hashtable or array in a file a recover it without any processing
= Description:This code is an example of use for the Data::Dumper module. It is available in all perl versions for any operating system.
= By: Bruno Martins
=
= Inputs:This piece of code reads a file named hash.txt (it does not need to exist) and prints it's content. after you are asked to enter a key/value pair and the code will store the hashtable in "hash.txt"
=
= Returns:When you run the code again it will show you the previous key/value pairs you entered
=
= Assumes:This code use of references, but you do not need to know anything about it, it is very clear and very easy to use.
=
=This code is copyrighted and has= limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=495&lngWId=6=for details.=**************************************

#!/usr/bin/perl
use Data::Dumper; # this module is used to store the hashtable in a way that can be recovered with the 'do' statement
# read the content of "hash.txt" to a variable (this variable will contain a reference to the hashtable)
$h= do "hash.txt";
# store the reference content into an hashtable
%hash=%{$h};
# prints the content of the hashtabele
print "previous inserted values:\n";
print "----------------------\n";
for (keys %hash)
{
	print "$_ => $hash{$_}\n";
}
	
print "----------------------\n";
# reads new key and value
print "new key:";
$key=<STDIN>;
chomp($key);
print "value for \'$key\':";
$value=<STDIN>;
chomp $value;
# stores the key and the value in the hashtable
$hash{$key}=$value;
# stores the hashtable in the file
open(A,">hash.txt");
print A Dumper \%hash; # don't forget to put the backslash before the hashtable, otherwise it won't work
close(A);
## DON'T FORGET TO VOTE


Other 3 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.