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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Hashes

Status
Not open for further replies.

MJAZ

Programmer
Joined
Aug 1, 2006
Messages
73
Location
US
How could I make it so that the value of a scalar is used in a hash? Here is my code:

Code:
my %contentfiles = [ login => $ENV{DOCUMENT_ROOT}."/templates/content/logincontent.txt",
                            passfnd => $ENV{DOCUMENT_ROOT}."/templates/content/passfndcontent.txt",
                            signup => $ENV{DOCUMENT_ROOT}."/templates/content/signupcontent.txt",
                            services => $ENV{DOCUMENT_ROOT}."/templates/content/servicescontent.txt",
                            compare => $ENV{DOCUMENT_ROOT}."/templates/content/comparecontent.txt",
                            capabilities => $ENV{DOCUMENT_ROOT}."/templates/content/capabilitiescontent.txt",
                            violation => $ENV{DOCUMENT_ROOT}."/templates/content/violationscontent.txt",
                            billing => $ENV{DOCUMENT_ROOT}."/templates/content/billingcontent.txt",
                            other => $ENV{DOCUMENT_ROOT}."/templates/content/othercontactscontent.txt",
                            about => $ENV{DOCUMENT_ROOT}."/templates/content/aboutcontent.txt",
                            history => $ENV{DOCUMENT_ROOT}."/templates/content/historycontent.txt" ];
        my $template_file = $ENV{DOCUMENT_ROOT}."/templates/main.tmpl";
        my $tmpl = new HTML::Template( filename => $template_file );

        my $dropdown = $ENV{DOCUMENT_ROOT}."/templates/content/dropmenu.txt";
        my $contentfile = read_file($contentfiles{$content});#The offending line
        my $dropmenu = read_file($dropdown);

The scalar ($content)is a $query->param(), and the hash is a list of possible values of the scalar and the text file it has to open in the case of each value. When I run the script, it either gives me a syntax error or an error that says the file doesn't exist, which it does. Any ideas?
 
your hash structure is not correct, you are using square brackets when you should be using parenthesis:

Code:
my %contentfiles = ( hash data here );

To add $content to the hash, add another key and assign it the value of string content:

Code:
$contentfiles{'keyname'}=$content;

if you want to check that $content is a value of one of the hash keys, and you do not know the name of the key, you have to loop through the hash:

Code:
foreach my $keys (keys %contentfiles) {
   if ($content eq $contentfiles{$keys}) {
      do something
   }
}

if you know the name of the key:

Code:
if ($contentfiles{'passfnd'} eq $content) {
   do something
}
 
Thanks. However, the whole point of this is to avoid like ten If... Else statements. I could use the If statements, but my code would be ugly and hard to work with. Any ideas for the workaround?
 
Okay, the error Iget is this:

Code:
read_file 'C:/Program Files/Apache Software Foundation/Apache2.2/templates/content/aboutcontent.txt' - sysopen: No such file or directory at C:\PROGRA~1\APACHE~2\Apache2.2\htdocs\index.cgi line 65

after I change the hash structure. I know the file exists, but it says that it does not. Is there an error in my code? The code is still the same but with parentheses instead of square brackets in the %contentfiles hash.
 
I copied your code and modified it a little for readability and for testing.
I created a stand alone script and set and evironment variable of "DOCUMENT_ROOT" (in my test case = "/zub") and ran the code.
The output looked fine to me.
See what you think:
Code:
#!/usr/bin/perl -w
use strict;
use HTML::Template;

my $rootpath     = $ENV{DOCUMENT_ROOT} . "/templates";
my $contentpath  = "$rootpath/content";
my %contentfiles = ( login        => "$contentpath/logincontent.txt",
                     passfnd      => "$contentpath/passfndcontent.txt",
                     signup       => "$contentpath/signupcontent.txt",
                     services     => "$contentpath/servicescontent.txt",
                     compare      => "$contentpath/comparecontent.txt",
                     capabilities => "$contentpath/capabilitiescontent.txt",
                     violation    => "$contentpath/violationscontent.txt",
                     billing      => "$contentpath/billingcontent.txt",
                     other        => "$contentpath/othercontactscontent.txt",
                     about        => "$contentpath/aboutcontent.txt",
                     history      => "$contentpath/historycontent.txt",
                   );
my $template_file = "$rootpath/main.tmpl";
my $dropdown      = "$contentpath/dropmenu.txt";
#my $tmpl          = new HTML::Template( filename => $template_file );
#my $contentfile   = read_file($contentfiles{$content});                  #The offending line
#my $dropmenu      = read_file($dropdown);

foreach my $content (keys %contentfiles) {
    print $contentfiles{$content}, "\n";
}
And the results of running it were:
Code:
/zub/templates/content/violationscontent.txt
/zub/templates/content/comparecontent.txt
/zub/templates/content/historycontent.txt
/zub/templates/content/servicescontent.txt
/zub/templates/content/signupcontent.txt
/zub/templates/content/logincontent.txt
/zub/templates/content/passfndcontent.txt
/zub/templates/content/othercontactscontent.txt
/zub/templates/content/capabilitiescontent.txt
/zub/templates/content/aboutcontent.txt
/zub/templates/content/billingcontent.txt
Keep us informed with any developments. :-)




Trojan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top