Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with CGI Hash manipulations... 1

Status
Not open for further replies.

Xaqte

IS-IT--Management
Oct 4, 2002
971
US
I'm trying to make a CGI that will allow users to identify who is at what phone extension when. I need to list all extension numbers (100 - 148), and allow users to click an extension and add their name. If they are already in, and click it again then it removes their names. I'm fairly new with hashes and I think this is where I'm getting stuck. Any help or other suggested methods would be greatly appreciated.
Code:
#!perl
     
use CGI;
use CGI::Carp qw/fatalsToBrowser/;
 
$query = new CGI;

print $query->header;
print $query->start_html("Extensions");
print "<H1> Extensions</H1>\n";
&print_prompt($query);
&do_work($query);
&print_tail;
print $query->end_html;
sub print_prompt {
   my($query) = @_;
   
my %extensions = (
      100 => "Mike Jones",
      101 => "",
      102 => "", 
      103 => "",
      104 => "",
      105 => "", 
      106 => "",
      107 => "",
      108 => "", 
      109 => "",
      110 => "",
      111 => "", 
      112 => "",
      113 => "",
      114 => "", 
      115 => "",
      116 => "",
      117 => "", 
      118 => "",
      119 => "",
      120 => "", 
      121 => "",
      122 => "",
      123 => "", 
      124 => "",
      125 => "",
      126 => "", 
      127 => "",
      128 => "",
      129 => "", 
      130 => "",
      131 => "",
      132 => "", 
      133 => "",
      134 => "",
      135 => "", 
      136 => "",
      137 => "",
      138 => "", 
      139 => "",
      140 => "",
      141 => "", 
      142 => "",
      143 => "",
      144 => "", 
      145 => "",
      146 => "",
      147 => "", 
      148 => "",
  );
    my $key;
    my $extensions; 
    print $query->start_form;
    print $query->textfield('name');

    foreach $key ( 100 .. 148)
    {
    print $query->submit('Action',"$key:$extensions{$key}");
    } 
    
print $query->endform;
   print "<HR>\n";
 	}
 
sub do_work {
    my($query) = @_;
    my(@values,$key);

print "<H2>Here are the current settings in this form</H2>";
    
    
    foreach $key ($query->param) {
	print "<STRONG>$key</STRONG> -> ";
	my $ls = $query->param($key);
	my @want = split(/:/, $ls);
	print "@want[0], @want[1]";
#THIS IS WHERE I'M STUCK, I DON'T KNOW HOW TO #MODIFY/CHANGE THE HASH
}
} 
sub print_tail {
    print <<END;
<HR>
END
    ;
}

Even a push in the right direction would be of great help right now. Thanks again for any help!
X
 
to modify / change the hash a simple reassignment per key is sufficient

Code:
$key="100";
$extensions{$key}="Xaqte";

is this what you're looking 4

--Paul


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
Sorry if I wasn't more specific. I'm looking on how I could approach this specific part:
When the button is clicked, the %extensions hash is checked for the value of that key. If the value is empty, write it into the hash so as to appear at the top of the page in that button's value. If the value exists do the opposite... remove it from the hash so as to appear at the top of the page. I tried this:
Code:
$extensions{$key}="Xaqte";
It doesn't error out, but it doesn't reflect the change in the button on the top. I thought as long as I had the button read it's value from the hash, then I would be ok. Thanks again for the help!

X
 
Code:
if (exists ($extensions{$key}) {
   $extensions{$key}="No one at present";
} else {
    $extenions{$key}=$new_value;
}

am i still missing the point?
--Paul


It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ... smack the fecker
 
So, if I added this:
Code:
my $nm = @want[1];
my $nm1 = @want[0];
  if (exists ($extensions{$nm}) {
        delete ($extensions{$nm};   
 }
 else {
    $extenions{$key} = $nm1;
}
This should work, right?

X
 
Ok, that didn't work. Nor did this:
Code:
my $nm = @want[1];
my $nm1 = @want[0];
if (@want[1]) {
	  if ($extensions{$key} == $nm) || ($extensions{$key} eq $nm) {
          delete ($extensions{$key};   
 	  }
 	  else {
	  print "Try again";
	  }
Thanks,
X
 
Just to type back (in my own words) what you're trying to accomplish (tell me if I'm wrong):

==========
You're looking to have a web page keep track of which rep is on which phone extension at any given time. The reps must use a web page to update their information, so you could have up to 49 different systems accessing this page at any given time.
==========

If that's the case, you cannot do this by setting up a hash inside the script itself. Instead, you should look into a DBM file to store your values. Here's an example on how to use one from the Programming Perl book.

Code:
use DB_File;

tie(%hash, "DB_File", $filename)     # Open database.
    or die "Can't open $filename: $!";

$v = $hash{"key"};                   # Retrieve from database.
$hash{"key"} = "value";              # Put value into database.
untie %hash;

- Rieekan
 
Rieekan, thanks for the response. I know that is a great idea. However, due to my inexperience with databases I would prefer another method. Do you have any other suggestions?
 
OK, the DB File is nothing but two flat files that Perl uses to store hash information. To set it up, just run a file like the following:

Code:
#! /usr/bin/perl

use strict;

my %extensions;
my $filename = 'e:/webdb/temp/extensions';

dbmopen %extensions, $filename, 0666 or die "Can't open database: $!";

for (my $i = 100; $i<=148; $i++)
{
	$extensions{$i} = '';
}

foreach my $key (sort keys %extensions)
{
	print "$key\t$extensions{$key}\n";
}

dbmclose %extensions;

Then your program just accesses this file and processes the information in the hash.

Code:
#! /usr/bin/perl

use strict;
use CGI;
use CGI::Carp qw/fatalsToBrowser/;

my %extensions;
my $filename = 'e:/webdb/temp/extensions';

dbmopen %extensions, $filename, 0666 or die "Can't open database: $!";

my $query = new CGI;

print $query->header;
print $query->start_html("Extensions");
print "<H1> Extensions</H1>\n";

&do_work($query);

&print_prompt($query);

&print_tail;

print $query->end_html;

sub print_prompt {
	my($query) = @_;

	my $key;
	my $extensions;
	print $query->start_form;
	print $query->textfield('name');
	print "<br>\n";

	foreach $key ( sort keys %extensions)
	{
		print $query->submit('Action',"$key:$extensions{$key}");
		print "<br>\n";
	}
 
	print $query->endform;
	print "<HR>\n";
}
 
sub do_work {
	my($query) = @_;
	my(@values,$key);

    my $ls = $query->param('Action');
    my @want = split(/:/, $ls);

	if ($extensions{$want[0]} ne $query->param("name"))
	{
		$extensions{$want[0]} = $query->param("name");
	}

	print "<H2>Here are the current settings in this form</H2>";

	foreach $key ( sort keys %extensions) 
	{
		print "<STRONG>$key</STRONG> -> ";
		print "$extensions{$key}\n";
	}
}

sub print_tail
{
	print "<HR>\n";
}

dbmclose %extensions;

- Rieekan
 
Wow, thanks Rieekan! This is definitely a step in the right direction. I've modified the code you given me, and I'm almost there. Here is how I have the "do_work" sub:
Code:
sub do_work {
    my($query) = @_;
    my(@values,$key);
 
    my $ls = $query->param('Action');
    my @want = split(/:/, $ls);
    
    if ($want[1]) {
    foreach $key ( sort keys %extensions) {
	    if ($key == $want[0]) {
		    $extensions{$want[0]} = "VACANT";
	    }
	    else {
	       $extensions{$want[0]} = $query->param("name");
            }
    }
    }
    else {
	       $extensions{$want[0]} = $query->param("name");
    }	    
    print "<H2>Here are the current settings in this form</H2>";

    foreach $key ( sort keys %extensions) 
    {
        print "<STRONG>$key</STRONG> -> ";
        print "$extensions{$key}\n";
    }
}
I don't know what I'm missing. Can you give me another push, please?
X
 
Rieekan,

looks like an FAQ in the making ... ;-)

--Paul

Nancy Griffith - songstress extraordinaire,
and composer of the snipers anthem "From a distance ...
 
Try this: It should work.

Code:
sub do_work {
    my($query) = @_;
    my(@values,$key);
 
    my $ls = $query->param('Action');
    my @want = split(/:/, $ls);
    
    if ($want[1])
	{
		foreach $key ( sort keys %extensions)
		{
			if (($key == $want[0]) && ($extensions{$key} =~ $query->param('name'))) 
			{
				$extensions{$want[0]} = "VACANT";
			}
			elsif (($key == $want[0]) && ($extensions{$key} !~ $query->param('name')))
			{
				$extensions{$want[0]} = $query->param('name');
			}
		}
    }
    else
	{
		$extensions{$want[0]} = $query->param("name");
    }        
    print "<H2>Here are the current settings in this form</H2>";

    foreach $key ( sort keys %extensions)
    {
        print "<STRONG>$key</STRONG> -> ";
        print "$extensions{$key}\n";
    }
}

Also, it might not be a bad idea to change the creation of your DB file to set the value of each key as 'VACANT' as you're doing in the actual script when a user wants to leave the phones.

Paul - I'd write a FAQ, but I should read up a little more before doing so. I'd hate to put something that's not 100% accurate in there. heh

- Rieekan
 
Thanks, Rieekan! I can't believe how easy you made for me to understand/work with databases.

X
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top