Hi guys can anyone help this novice with what is probably a very simple error/misunderstanding about using a reference for a hash table. I'm converting some UNIX shell scripts to Perl but depending on where the script is run a different environment is required. I've tried using Tilly's script to ingest the local environment but it refused to work on our site. Instead I've created a subroutine with 3 huge hash tables containing these different environments and depending on a parameter supplied the appropriate hash table will be walked and the environment set up accordingly. The first problem that I encountered was how to use the parameter to pick out the correct hash table. I've got round this by setting up a reference to each hash table. This works and the correct table is being called but I'm getting an error that I think is due to the way I'm trying to use this reference but.......I'm not sure how to resolve this? Can someone point out the error of my ways or is there a more obvious way to access these tables? The code below is the actual code that I've been testing minus about 800 hash table elements - it's not working for any elements!
#!/usr/bin/perl -w # ####################################### # Perl Modules Used #-------------------------------------- use strict ; use Cwd ; # Current Working + Directory Module use Mail::Sender; # Email module use lib '/home/interface/scripts/Perl_Modules' ; # Where ACC_Vario +us lives # sub ACC_IHS_PROFILE { use strict ; # # # Scalar Variables #-----------------# my $db = undef ; my $DB_hash = undef ; my $key = undef ; my $paramno = undef ; my $value = undef ; # # Array Variables #---------------# # # Boolean Variables #-----------------# my $PARAMNO = 0 ; my $PARAMETER_ERROR = 0 ; # # HASH Table for LIVE environment #-------------------------------# my %LIVE = ( "ADMIN_DIR" => "/opt/bin1", ); # # HASH Table for DEVLP environment #--------------------------------# my %DEVLP = ( "ADMIN_DIR" => "/opt/bin2", ); # # HASH Table for TEST environment #--------------------------------# my %TEST = ( "ADMIN_DIR" => "/opt/bin3", ); # my %DBhashlookup = ( "TEST" => [\%TEST], "LIVE" => [\%LIVE], "DEVLP" => [\%DEVLP], ) ; # P R O C E S S I N G #-------------------# $paramno = (@_) ; # if ($paramno == 0) { print "\n\tNo parameters supplied - can't set up environment!\n" + ; $PARAMETER_ERROR = 1 ; } if ($paramno > 1) { print "\n\tToo many parameters supplied - Highlander!\n" ; $PARAMETER_ERROR = 1 ; } if (! $PARAMETER_ERROR) { $db = $_[0] ; if (($db ne "LIVE") && ($db ne "TEST") && ($db ne "DEVLP")) { print "\n\tDB $db is not known to this script\n" ; $PARAMETER_ERROR = 1 ; } } if (! $PARAMETER_ERROR) { $DB_hash = $DBhashlookup{$db} ; while (($key, $value) = each %$DB_hash ) { print "\n\tKEY :: $key\n" ; print "\n\tVALUE :: $value\n" ; $ENV{$key} = $value ; } } if ( $PARAMETER_ERROR ) { return 0 ; } else { return 1 ; } # } # # Scalar Variables #----------------# my $db = 'DEVLP' ; my $key = undef ; my $script = "xxrctest" ; my $value = undef ; # # Array Variables #----------------# # # Boolean Variables #-----------------# print "\n\t$script S T A R T S\n" ; &ACC_IHS_PROFILE($db) or die "\n\tBottom - Sub Failed!\n" ; print "\n\t$script E N D S\n" ;

The error message looks like an unwinding error to a numpty like me but here it is anyway.
xxrctest S T A R T S Argument "/opt/bin2" isn't numeric in each at ACC_IHS_PROFILE.pl line +85. Bad index while coercing array into hash at ACC_IHS_PROFILE.pl line 85 +.

Please let me know what folly I'm commiting here as I can't see it and it's begining to drive me insane(r)! Cheers, Ronnie

In reply to Hash Table References? by Ronnie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.