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

Add a another value to a key in a hash

Status
Not open for further replies.

blues77

Programmer
Jun 11, 2002
230
CA
Hello,

Right now I have a hash with keys that have values but if I want to add another value to the hash key it overwrites the value already stored there. Is there anyway to make it so that I can store more than one value for a particular key? Below is the code that I have currently.


Code:
if($line2 =~ /^($line1)\s+CAAS.+\.([^.]+)\.([^.]+)/ )
			{
				$flag = 1;
				my $keyExists = 0;
				my $valueExists = 0;
				while ( my ($key, $value) = each(%hash) ) 
				{
			        	if($key EQ $2)
					{
						print "key = $key  two = $2\n";
						$keyExists = 1;
						if($value  EQ $3)
						{
							print "value = $value  three = $3\n";
							$valueExists = 1;
						}
					}
				}
				
				print "keyExists $keyExists\n";
				print "valueExists $valueExists\n";
				if($keyExists == 1 && $valueExists == 0)
				{
					$hash{$2} = $3;
				}
				if($keyExists == 0 && $valueExists == 0)
				{
					$hash{$2} = $3;
				}
				
				$keyExists = 0;
				$valueExists = 0;
				print "keyExists $keyExists\n";
				print "valueExists $valueExists\n";
				#$hash{$2} = $3;
				#print ("Found the string $line1 in the file $ARGV[1]\n");			
				#print ("Segment = $2 and COBOL Field Name = $3\n");
			}

So the problem here arises when
Code:
if($keyExists == 1 && $valueExists == 0)
				{
					$hash{$2} = $3;
				}

Or in other words the key exists but the value being examined doesn't. When the value is then added the value that was already present there is over written. All help is course very much appreciated!

Thanks
 
For example:
Code:
if($keyExists == 1 && $valueExists == 0)
                {
                    push @{ $hash{$2} }, $3;
                }
or something like that. :)

Corwin
 
Code:
#!/usr/bin/perl -w
use strict;

my %hash;

foreach (1..4) {
   push @{ $hash{key} },  $_;
}

foreach (0 .. @{ $hash{key} } - 1) {
   print "$hash{key}[$_]\n";
}

Code:
#!/usr/bin/perl -w
use strict;

my %hash;
use Data::Dumper;

foreach (1 .. 4) {
   push @{ $hash{key1} },  $_;
}


foreach (5 .. 9) {
   push @{ $hash{key2} },  $_;
}
my $temp = \%hash;
print Dumper($temp),"\n";

Execute and examine these two scripts, to see what hashes of arrays look like. Sorry but don't have time for more. Have a nice eve. :)

Corwin
 
Humm I tired what you sugested and I'm getting the error
Can't use string ("SIN_TTN") as an ARRAY ref while "strict refs" in use at extract_label.pl line 49,

In this case SIN_TTN is suuposted to be a value that I want to add under once of the existing keys that already contains values. Anyways if I remove the use strict; command then things go through, however, the second value is not added under the key. Any idea what I'm doing wrong?

Here is the code I hace

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)
					{
						print "key = $key  two = $2\n";
						$keyExists = 1;
						if($value  EQ $3)
						{
							print "value = $value  three = $3\n";
							$valueExists = 1;
						}
					}
				}
				
				print "keyExists $keyExists\n";
				print "valueExists $valueExists\n";
				if($keyExists == 1 && $valueExists == 0)
				{
					
					push @{ $hash{$2} },  $3;
					#$hash{$2} = $3;
				}
				if($keyExists == 0 && $valueExists == 0)
				{
					$hash{$2} = $3;
				}
				
				$keyExists = 0;
				$valueExists = 0;
				print "keyExists $keyExists\n";
				print "valueExists $valueExists\n";
				#$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 (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;

Thanks for your help!
 
Ok I've made some more modifications and the error I was getting is gone but has been replaced by.

Bareword "search" not allowed while "strict subs" in use at extract_label.pl line 102.
Execution of extract_label.pl aborted due to compilation errors.

I changed how the hash should be printing out all of it's values as shown here
Code:
foreach (keys %hash) 
		{
			remove all whitespace using search and replace
			$_ =~ s/ //g;
			chomp;
			print OUTPUT "$_,";
		}
		
		print OUTPUT "\t";

		foreach (keys %hash)
		{
			my $key = $_;
			foreach (0 .. @{ $hash{$key} } - 1) 
			{
				print "$hash{$key}[$_]\n";
			}
		}

I think this should work except for that error message I'm getting. Any thoughts? I'll be glad when this is over.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top