Trouble with $key to HASH when Numeric

C

CowBoyCraig

It seems if I "Change" the $key going to if ""(exists($GREEN{$key}))""
at all It hoses. The keys look like "19973|3.1.A.4" without the quotes.


If I print like this "print GREEN{$key}\n"; to see what the Hash is
getting it looks like this:
}REEN{12483|3.1.B.2

That is odd? Is there a way to send my data to the hash without it
getting hosed???

Code below:


foreach (keys %HOT) {

# 6AE-23945: 4.1.B.6 # Fix this
# Make a Key for %GREEN from %HOT that works
my $i=$_;
$i=~/(\d{5}?)$/g;
$i=$1;

my $key="$i|$HOT{$_}";
#print "$GREEN{$key}\n";

if (exists($GREEN{$key})) {
# Remove from %GREEN
# remove $KEY and its value from %HASH
print "Removed $GREEN{$key}\n";
delete($GREEN{$key});
} else { # print "did not see\n";
}
}


Craig
 
J

Joe Smith

CowBoyCraig said:
It seems if I "Change" the $key going to if ""(exists($GREEN{$key}))""
at all It hoses. The keys look like "19973|3.1.A.4" without the quotes.

Based on your problem, I can see that they are not what you think.
If I print like this "print GREEN{$key}\n"; to see what the Hash is
getting it looks like this:
}REEN{12483|3.1.B.2

That is to be expected when you print a value like "12483|3.1.B.2\r\n".

You did not use chomp() or other means to get rid of the
unwanted carriage return at the end of the line.

Next time, post to comp.lang.perl.misc instead of comp.lang.perl .

-Joe
 
J

Jim Gibson

CowBoyCraig said:
It seems if I "Change" the $key going to if ""(exists($GREEN{$key}))""
at all It hoses. The keys look like "19973|3.1.A.4" without the quotes.

Can you be more specific? "hoses" is not a good description of an error
condition.
If I print like this "print GREEN{$key}\n"; to see what the Hash is
getting it looks like this:
}REEN{12483|3.1.B.2

Looks like your key has a carriage return ('\r') at the end.
That is odd? Is there a way to send my data to the hash without it
getting hosed???

One does not "send data" to a hash. Can you be more specific about what
you are trying to do?
Code below:
use strict;
use warnings;
foreach (keys %HOT) {

# 6AE-23945: 4.1.B.6 # Fix this
# Make a Key for %GREEN from %HOT that works
my $i=$_;
$i=~/(\d{5}?)$/g;

What do you think this statement is doing? It has a couple of oddities:
1) the pattern /(\d{5}?)$/ will match zero or one group of 5 digits at
the end of a string -- in other words, this pattern will always match
and capture either 5 digits or nothing; 2) the pattern is anchored to
the end of the string, yet you are asking for all matches with the g
modifier (there can only be one end of string unless you specify the m
modifier, which you do not).

You may have 5 digits in $1, or you may have nothing. You should check
to see which it is (or fix your regex).
my $key="$i|$HOT{$_}";
#print "$GREEN{$key}\n";

if (exists($GREEN{$key})) {
# Remove from %GREEN
# remove $KEY and its value from %HASH
print "Removed $GREEN{$key}\n";
delete($GREEN{$key});
} else { # print "did not see\n";
}
}

Please post a complete, working program and tell us what you think it
is doing that it is not. But post it to comp.lang.perl.misc, because
this newsgroup is defunct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top