Ok I'm having absolutely no luck trying to get this hash of array thing to work. As you can see, from the code below, I've tried several different ways to try and input several values for a specific key, if the $keyExists == 1 and $valueExists == 0 conditions are met. I thought it might be working but that I wasn't printing out the values properly so therefore it looked liek nothing was in the hash but I'm not sure. I've tried several different techniques but none seem to work. I'm now back to the point where I just have a normal hash. Below is the code I have with the different attempts I've tried. Can anyone make a suggestion as to how I can get this to work?
Thanks
Thanks
Code:
use strict;
#print "$ARGV[0]";
open(MY_FILE1,$ARGV[0]);
open(OUTPUT, ">>output.txt");
while(<MY_FILE1>)
{
chomp;
my $line1 = $_;
my %hash = ();
my $flag = 0;
my $view = 0;
my $noCAASsource = 0;
open(MY_FILE2,$ARGV[1]);
while(<MY_FILE2>)
{
chomp;
my $line2 = $_;
if($line2 =~ /^\s*$/)
{
#do nothing
}
elsif($line2 =~ /^($line1)\s+/)
{
if($line2 =~ /^($line1)\s+CAAS.+\.([^.]+)\.([^.]+)/ )
{
$flag = 1;
my $keyExists = 0;
my $valueExists = 0;
while ( my ($key, $value) = each(%hash) )
{
if($key EQ $2)
{
$keyExists = 1;
if($value EQ $3)
{
$valueExists = 1;
}
}
}
if($keyExists == 1 && $valueExists == 0)
{
print "key exists adding new value \n";
#push @{ $hash{$2} }, $3;
$hash{$2} = $3;
}
if($keyExists == 0 && $valueExists == 0)
{
print "nothing exists adding new value\n";
$hash{$2} = $3;
}
$keyExists = 0;
$valueExists = 0;
#$hash{$2} = $3;
#print ("Found the string $line1 in the file $ARGV[1]\n");
#print ("Segment = $2 and COBOL Field Name = $3\n");
}
if($line2 =~/^($line1)\s+View/)
{
$view = 1;
}
if($line2 =~/^($line1)\s+NO CAAS Source/)
{
$noCAASsource = 1;
}
}
}
if($flag == 0 && $view == 0 && $noCAASsource == 0)
{
print "nothing found\n";
print OUTPUT "nothing found\n";
}
elsif($flag == 0 && $view == 1)
{
print "view\n";
}
elsif($flag == 0 && $view == 0 && $noCAASsource == 1)
{
print "NO CAAS Source\n"
}
elsif($flag == 1 && $view == 0)
{
print keys %hash, ;
print "\n";
print "$line1 has a segment and COBOL field\n";
}
elsif($flag == 1 && $view == 1)
{
foreach (keys %hash)
{
#remove all whitespace using search and replace
$_ =~ s/ //g;
chomp;
print OUTPUT "$_,";
}
print OUTPUT "\t";
#foreach $key ( keys %hash )
#{
# print OUTPUT "@{$hash{$key}}\n"
#}
#foreach (keys %hash)
#{
# my $key = $_;
# foreach (0 .. @{ $hash{$key} } - 1)
# {
# print "$hash{$key}[$_]\n";
# }
#}
foreach (values %hash)
{
#remove all whitespace using search and replace
$_ =~ s/ //g;
chomp;
print OUTPUT "$_,";
}
print OUTPUT "\n";
print "View $line1 has a segment and COBOL field\n";
}
$noCAASsource = 0;
$flag = 0;
$view = 0;
close MY_FILE2;
}
close OUTPUT;
close MY_FILE1;
close MY_FILE2;