From: J. Gleixner on
ccc31807 wrote:
> This is embarrassing!
>
> I read an input file, split each row on the separators, pick out the
> unique key (which consists of seven digits), create a hash element on
> the key, and read the other values into an anonymous hash. This has
> worked for years without a hitch.
>
> Yesterday, my user requested the app to print a calculated field,
> which is a list of names contained in a secondary file. I thought, "No
> problem, I'll just create a new element in my anonymous hash,
> concatenate each name unless the name already matched the string, and
> print it out." Didn't work, but the errors seemed to be random and
> arbitrary.
>
> After a couple of hours, I used bdf's trick to print out the hash, and
> discovered much to my embarrassment that the keys weren't the same. In
> the main loop where I created the hash, the keys were as expected and
> consisted of seven characters which are all digits. HOWEVER, in the
> loop where I created the additional hash element, the keys with
> leading zeros did not match. Here is an example of the hash:

Most folks really don't need to know the backstory. Post your code,
your results, your expectations, and your questions.
From: Ben Morrow on

Quoth ccc31807 <cartercc(a)gmail.com>:
> On Jun 8, 2:26�pm, "Uri Guttman" <u...(a)StemSystems.com> wrote:
> > dunno what your code is doing (as you didn't post any) to make this
> > happen. are you doing an eval on some incoming data? that is a no-no! do
> > a proper parse and you can keep those keys as strings.
>
> This is what I'm doing.
>
> The main file looks like this:
> 0123456|joe|male|etc ...
>
> which I manipulate as follows:
> my ($id, $name, $gender, @rest) = split /\|/;
> $main_hash{$id} = {

'%main_hash' is an appallingly bad name for a variable. Why is it there?
What's it got in it? (In this case, probably, something like '%people'
might be better.)

> id => $id,
> name => $name.
> gender => $gender,
> rest => @rest,

This line does not do what you think it does.

> new_value => ' ',
> };
>
> The secondary file looks like this:
> 0123456|this|etc ...
> 0123456|is|etc ...
> 0123456|a|etc ...
> 0123456|list|etc ...
> 0123456|of|etc ...
> 0123456|strings|etc ...
>
> which I manipulate as follows:
> my ($id, $string, @rest) = split /\|/;
> $main_hash{$id}{new_value} .= $string unless $main_hash{$id}
> {new_value} =~ /$string/; #the strings can be duplicated but I only

You want \Q\E here.

> want one of each
>
> So, should I double quote the $id when I use it as a hash key? That
> strikes me as idiosyncratic even for Perl.

Works for me:

~% perl -E'
my %main_hash;
{
my $line = "0123456|joe|male|etc";
my ($id, $name, $gender) = split /\|/, $line;
$main_hash{$id} = {
id => $id,
name => $name,
gender => $gender,
new_value => " ",
};
}
for my $line ("0123456|this|etc", "0123456|is|etc") {
my ($id, $string) = split /\|/, $line;
$main_hash{$id}{new_value} .= $string
unless $main_hash{$id}{new_value} =~ /\Q$string/;
}
for (keys %main_hash) {
say "$_: $main_hash{$_}{new_value}";
}
'
0123456: this

(There's only one entry because "this" =~ /is/.)

So you must be doing something else.

Ben

From: ccc31807 on
On Jun 8, 3:18 pm, "J. Gleixner" <glex_no-s...(a)qwest-spam-no.invalid>
wrote:
> Provide actual code that we can run that shows your issue and so we can
> see what's happening.

Here is the working code in my test script. Unfortunately, I can't
post the data files. Note that this code does some other things and
contains debugging statements.

open FAC, '<', "FAC_${term}.csv", or die "Cannot open FAC, $!";
chomp ($header = <FAC>);
while (<FAC>)
{
next unless /\w/;
chomp;
my ($changed, $last, $first, $middle, $id2, $region, $contract,
$addy1, $addy2, $csz, $mail, $trs, @courses) = parse_line(',', $bool,
$_);
print "id2 is [$id2] and facid is [$facid]\n";
if ($facid !~ /\?/) { next unless $id2 eq $facid; }
$fac{$id2} = {
id2 => $id2,
contract => $contract,
first => $first,
middle => $middle,
last => $last,
addy1 => $addy1,
addy2 => $addy2,
csz => $csz,
mail => $mail,
trs => $trs,
courses => @courses,
xlist => '',
}
}
close FAC;

open SEC, '<', "$sections_file", or die "Cannot open SEC, $!";
chomp ($header = <SEC>);
while (<SEC>)
{
next unless /\w/;
chomp;
s/'/\\'/g;
my ($last, $first, $middle, $id1, $filename, $crs_id, $site, $loc,
$glcode, $level, $count, $status, $section, $title, $hours, $xlist,
$total, $travel, $contract) = parse_line(',', 0, $_);
next if $contract =~ /N/;
$sec{$crs_id} = {
crs_id => $crs_id,
filename => $filename,
id1 => $id1,
site => $site,
loc => $loc,
glcode => $glcode,
level => $level,
count => $count,
status => $status,
section => $section,
title => $title,
hours => $hours,
xlist => $xlist,
total => $total,
travel => $travel,
contract => $contract,
};
$xlist{$id1} .= "$section " if $xlist =~ /\d/ and $xlist !~ /
$section/;
$fac{$id1}{xlist} .= "$section " if $xlist =~ /\d/;
}
close SEC;



From: Uri Guttman on
>>>>> "c" == ccc31807 <cartercc(a)gmail.com> writes:

c> On Jun 8, 2:26�pm, "Uri Guttman" <u...(a)StemSystems.com> wrote:
>> dunno what your code is doing (as you didn't post any) to make this
>> happen. are you doing an eval on some incoming data? that is a no-no! do
>> a proper parse and you can keep those keys as strings.

c> This is what I'm doing.

c> The main file looks like this:
c> 0123456|joe|male|etc ...

c> which I manipulate as follows:
c> my ($id, $name, $gender, @rest) = split /\|/;
c> $main_hash{$id} = {
c> id => $id,
c> name => $name.
c> gender => $gender,
c> rest => @rest,

that is wrong as it will put the whole array there. you need a ref to
that array or an anon ref.

c> new_value => ' ',
c> };

c> The secondary file looks like this:
c> 0123456|this|etc ...
c> 0123456|is|etc ...
c> 0123456|a|etc ...
c> 0123456|list|etc ...
c> 0123456|of|etc ...
c> 0123456|strings|etc ...

c> which I manipulate as follows:
c> my ($id, $string, @rest) = split /\|/;
c> $main_hash{$id}{new_value} .= $string unless $main_hash{$id}
c> {new_value} =~ /$string/; #the strings can be duplicated but I only
c> want one of each

c> So, should I double quote the $id when I use it as a hash key? That
c> strikes me as idiosyncratic even for Perl.

in that limited code i don't see where the keys would be interpreted as
literal numbers. there must be something else going on which is doing
that. perl won't lose leading zeroes in strings without doing some
number conversions. are you sure you never look at those kays as
numbers? like use == to check them or similar? since they seem to be
fixed size you can always use the string comparison ops safely.

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: Uri Guttman on
>>>>> "c" == ccc31807 <cartercc(a)gmail.com> writes:

c> On Jun 8, 3:18�pm, "J. Gleixner" <glex_no-s...(a)qwest-spam-no.invalid>
c> wrote:
>> Provide actual code that we can run that shows your issue and so we can
>> see what's happening.

c> Here is the working code in my test script. Unfortunately, I can't
c> post the data files. Note that this code does some other things and
c> contains debugging statements.

c> open FAC, '<', "FAC_${term}.csv", or die "Cannot open FAC, $!";
c> chomp ($header = <FAC>);
c> while (<FAC>)
c> {
c> next unless /\w/;
c> chomp;
c> my ($changed, $last, $first, $middle, $id2, $region, $contract,
c> $addy1, $addy2, $csz, $mail, $trs, @courses) = parse_line(',', $bool,
c> $_);
c> print "id2 is [$id2] and facid is [$facid]\n";
c> if ($facid !~ /\?/) { next unless $id2 eq $facid; }
c> $fac{$id2} = {
c> id2 => $id2,
c> contract => $contract,
c> first => $first,
c> middle => $middle,
c> last => $last,
c> addy1 => $addy1,
c> addy2 => $addy2,
c> csz => $csz,
c> mail => $mail,
c> trs => $trs,
c> courses => @courses,

same bug as i pointed out in another post. you need a ref or anon array
there. that is very wrong. who knows what it is doing to your app?

c> xlist => '',
c> }

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4 5 6 7
Prev: How to read a given number of lines?
Next: something stupid