response inline below

John W. Krahn wrote:
Noah wrote:
Hi there,

Hello,

I have a routine returning a perl reference and I am trying to figure out how to properly use the hash tables in the main section of my perl proggie.

--- from the main program

my $templateConfiguation = &readConfigTemplate($templateConfigFilename, $sortedTemplateConfigFilename);

.... and later on how do I properly use the hash $templateConfiguation ....

        } else {
print DIFF "d> $key" if !exists ($templateConfiguation{$templateType}{$key}); print MATCH "m> $key" if exists ($templateConfiguation{$templateType}{$key});

if ( exists $templateConfiguation->{ $templateType }{ $key } ) {
    print MATCH "m> $key";
    }
else {
    print DIFF "d> $key";
    }



okay so I get a peculiar error message that I am not sure how to correct:


--- snip ---


Can't use string ("") as a HASH ref while "strict refs" in use at ./compare.configs.pl line 900.



--- line 900 to line 904 ---

            if (exists $templateConfiguation->{$templateType}{$key}) {
                print MATCH "m> $key";
            } else {
                print DIFF "d> $key";
            }


cheers,

Noah





----- from the subroutine ----

sub readConfigTemplate {
    my $templateConfigFilename = shift;
    my $sortedTemplateConfigFilename = shift;
    my %templateConfiguation = ();

    ### Read template file
open (DATA, $templateConfigFilename) || die("Could not open file $templateConfigFilename!");

You should include the $! variable in the error message so you know *why* open failed.

    ### save output of sorted template file
open (OUTPUT, ">$sortedTemplateConfigFilename") || die("Could not open file $sortedTemplateConfigFilename!");

You should include the $! variable in the error message so you know *why* open failed.

    @lines = <DATA>;
    @lines = sort (@lines);

No need for two assignments:

      my @lines = sort <DATA>;

    my $linecount = 0;

You never actually use this variable?

    my $templateType = undef;

    ### decide if the template type is blah1 or blah2
    for my $line (@lines) {
        if ($templateConfigFilename =~ /blah1/ && !$templateType) {
            $templateType = "blah1";
            last;
        } elsif ($templateConfigFilename =~ /blah2/ && !$templateType) {
            $templateType = "blah2";
            last;
        }
    }

Or perhaps:

    for my $line ( @lines ) {
if ( $templateConfigFilename =~ /(blah[12])/ && !defined $templateType ) {
            $templateType = $1;
            last;
        }
    }

    ### make sure some $templateType is defined
                        *^^^^^^^^^^^^^^^^^^^^^^^^*

die ("Someting broken in $templateConfigFilename\n") if !$templateType;

die ("Someting broken in $templateConfigFilename\n") if !defined $templateType;

    foreach my $line (@lines) {
        #### remove end line spacing
                     *^^^^^^^^^^^^^^^^*
#### remove a single carriage return from somewhere in the string

        $line =~ s/\r//;
        #### remove start line spacing
        $line =~ s/^\s+//;

        next if $line !~ /^blahblah/;
        print OUTPUT $line;
        $templateConfiguation{$templateType}{$line} = $line;
        $linecount++;

You don't actually use $linecount anywhere.

    }
    close (DATA);
    close (OUTPUT);
    return \%templateConfiguation;
}


John

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to