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";
}
----- 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
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/