PDA

View Full Version : Add/Delete Hash Key/Value then Show Hash


NiteOwl
03-22-2007, 09:55 PM
I Just don't see what is wrong.
Thanks for looking...

I am trying to add/delete a hash key/value and then display it in a drop down menu.

It works, second time arround.
I will remove the # in test.cgi to add a key/value , then run showhash.

It displays the hash before the change, if i run it again it shows it updated.

Same thing with delete key/value.



data file cattype.txt


%content = ( mobile => "Mobile Home",
single => "Single Family",
modular => "Modular",
condo => "Condo",
featured => "Featured",
cland => "Commercial Land",
cprop => "Commercial",
rland => "Residential Land");





Test CGI :


#!/perl/bin/perl
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

print "Content-type: text/html\n\n";


$newitem = "newvalue";
$newkey = "neykey";
$hashfilename = "cattype";


##################################################
#
# To Test i have been removing the # on the statments in bold
#
#
#
##################################################

require "edithash.pm"; # has add/delete/show hash code

# &deletehash($hashfilename , $newkey);
# &addhash($hashfilename , $newitem, $newkey);


&showhash; # This will display the hash as a dropdown

##################################################




edithash.pm


#######################################################
# Prints Dropdown
#
#
#######################################################

sub showhash {
require "gethashselect.pm";

$showthishash = gethashselect($hashfilename);
print qq~<select name="variable name">~;
print $showthishash;
print qq~</select>~;
}

#######################################################



#######################################################
#
# Add Key and Value to Hash
#
#######################################################
sub addhash {
my $val1=$_[0];
my $Newha****em=$_[1];
my $Newhashkey=$_[2];
my $filename = $val1;
my $fileext = ".txt";


require "settings/system/$filename$fileext";
%Thishash = %content;

if ($Newha****em ne "") {
$Thishash{$Newhashkey} = $Newha****em;
}

open(FILE,">settings/system/$filename$fileext") or &dienice("Couldn't open $filename$fileext: $!");

$linelen = scalar keys(%Thishash);
$count = 0;

if ($linelen eq 0){ print FILE "\%Thishash = \(\"\"\)\;\n"; }
else {
print FILE "\%content = \(";

# $count++;

foreach my $i (keys %Thishash) {
print FILE qq~ $i \=\> \"$Thishash{$i}\"~;
$count++;
if ($count < $linelen) { print FILE "\,\n"; }
#if ($linelen-1 > $i) { print "\,"; }
}
print FILE "\)\;\n";
}

close(FILE);
}



#######################################################
#
# Deletes Key, Value from Hash
#
#######################################################

sub deletehash {
my $val1=$_[0];
my $Newhashkey=$_[1];
my $filename = $val1;
my $fileext = ".txt";


require "settings/system/$filename$fileext";
%Thishash = %content;

if ($Newhashkey ne "") {
delete $Thishash{$Newhashkey};
}


open(FILE,">settings/system/$filename$fileext") or &dienice("Couldn't open $filename$fileext: $!");

$linelen = scalar keys(%Thishash);
$count = 0;

if ($linelen eq 0){ print FILE "\%Thishash = \(\"\"\)\;\n"; }
else {
print FILE "\%content = \(";

# $count++;

foreach my $i (keys %Thishash) {
print FILE qq~ $i \=\> \"$Thishash{$i}\"~;
$count++;
if ($count < $linelen) { print FILE "\,\n"; }
#if ($linelen-1 > $i) { print "\,"; }
}
print FILE "\)\;\n";
}

close(FILE);
}



83;





gethashselect.pm




##########################################
# Get Hash Select
# Returns Dropdown Menu
#
# $Var = gethashselect(cattype);
#
##########################################

sub gethashselect {
$selectdata = "";
$selectfile="";
$selecttype="";
$selectfile=$_[0];
%hashdata = ();



##########################################
# Main Logic
##########################################

%hashdata = &loadhash($selectfile);
&loadselectdata;
return ($selectdata);

##########################################
# End Main Logic
##########################################







##########################################
# Get Array from file
#%hashdata = &loadhash($Filename);
#########################################
sub loadhash {
my $filename=$_[0];
my $fileext = ".txt";

require "settings/system/$filename$fileext";
return(%content);
}
##########################################




##########################################
# Create Dropdown menu
##########################################
sub loadselectdata {

foreach my $i (keys %hashdata) {
chomp($hashdata{$i});
$selectdata=$selectdata."\<option Value=\"$i\">$hashdata{$i} \n";
}

}
##########################################


} #end getselect


78;




Thank you for your time...

KevinADC
03-23-2007, 01:02 AM
I think you should look into the Storable module of you want persistent perl data.

NiteOwl
03-23-2007, 01:29 AM
if i call add hash or delete hash
it works fine.

then if i call show hash. it does.
no problems.

if i call add or delete with show... i get the hash before change.

somehow, i am getting a conflict in the require statement when i load %content.

FishMonger
03-23-2007, 05:44 AM
What are you trying to accomplish? Is this a real project you're working on or just a learning exercise?

You should start by throwing that away and starting over from scratch.

Your stating point should be to read up on how to write a proper module.

Writing Perl Modules for CPAN - Book by Sam Tregar, freely available from Apress
http://www.google.com/url?sa=t&ct=res&cd=2&url=http%3A%2F%2Fwww.apress.com%2Ffree%2Flinuxworld%2Fgetbook.html%3Fbid%3D1&ei=0kkDRqKPLJyuggO4sKmMBA&usg=___YUgMAbu2HmhN8RN7MQlz9WanzM=&sig2=THEs4MJb8NT68OjI3huO1Q

General google search: writing perl modules
http://www.google.com/search?num=20&hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=iEa&pwst=1&sa=X&oi=spell&resnum=0&ct=result&cd=1&q=writing+perl+modules&spell=1

NiteOwl
04-02-2007, 02:12 AM
Problem solved.

I was looking at the hash before it was updated.
Simple fix. Once i walked away for a days.

After I re-wrote the pieces of testing code it worked fine.

Fish,
It is a new addition to working project.

FishMonger
04-02-2007, 05:44 AM
I'm glad to hear that you solved the problem, but I really think you should look at redesigning your implementation. I really don't mean to be rude, but the module implementation that you should us is really bad and should be redesigned. You should look at using the storable module as Kevin as suggested or design your own properly written module(s).