S
S
Hi,
In advance sorry for the lengthy email. I'm new at using Perl and I have
never posted here so my goal is to hopefully provide as much information as
neccessary. I provided:
- a broken test script exhibiting a problem (along with the output)
- a fixed test script no longer exhibiting the problem (along with the
output)
- a quick description of my environment
- a list of what I tried
One could wonder why I'm posting here since I already have 'a fix'. Well, I
would like to understand why moving this specific line of code fixed the
problem. Specifically, I want to understand why it seems that a 'hash' can
be alternatively 'initialized' or 'uninitialized' in the broken script. It
just does not make sense to me with my limited knowledge.
In my example, assuming that %messages turned out to be a very big hash,
what would be the impact of rebuilding the @sortedmessages array every time
in the loop as I'm doing in the 'fixed' test script? Yes, I know that it
sounds like I want to optimize before I even know how to code. Here again,
it is just for my own 'edification'. Thanks for reading the rest of the mail
and teaching me along.
Note 1: the script is written for learning purpose only, you are welcome to
comment on it also.
Note 2: English is not my native language and I'm trying really hard to be
understood on this forum.
The test script exhibiting a problem:
=========================
use strict;
use warnings;
my %messages;
$messages{"C"}="333";
$messages{"B"}="222";
$messages{"A"}="111";
my @sortedmessages = map { { ($_ => $messages{$_}) } } sort keys %messages;
my $maxi=2;
my $message_element;
my $key = my $value;
foreach my $i (0...$maxi)
{
foreach $message_element (@sortedmessages)
{
($key, $value) = each %$message_element;
print ("key is $key and value is $value\n");
}
}
Output with the broken script:
=====================
key is A and value is 111
key is B and value is 222
key is C and value is 333
Use of uninitialized value in concatenation (.) or string at
D:\test.pl line 16.
Use of uninitialized value in concatenation (.) or string at
D:\test.pl line 16.
key is and value is
Use of uninitialized value in concatenation (.) or string at
D:\test.pl line 16.
Use of uninitialized value in concatenation (.) or string at
D:\test.pl line 16.
key is and value is
Use of uninitialized value in concatenation (.) or string at
D:\test.pl line 16.
Use of uninitialized value in concatenation (.) or string at
D:\test.pl line 16.
key is and value is
key is A and value is 111
key is B and value is 222
key is C and value is 333
The 'fixed' test script no longer exhibiting a problem
=====================================
use strict;
use warnings;
my %messages;
$messages{"C"}="333";
$messages{"B"}="222";
$messages{"A"}="111";
my $maxi=2;
my $message_element;
my $key = my $value;
foreach my $i (0...$maxi)
{
#line 7 from broken script was moved here and fixed the problem
my @sortedmessages = map { { ($_ => $messages{$_}) } } sort keys
%messages;
foreach $message_element (@sortedmessages)
{
($key, $value) = each %$message_element;
print ("key is $key and value is $value\n");
}
}
Output with fixed script:
=====================
key is A and value is 111
key is B and value is 222
key is C and value is 333
key is A and value is 111
key is B and value is 222
key is C and value is 333
key is A and value is 111
key is B and value is 222
key is C and value is 333
Current environment:
==============
ActiveState Perl v5.8.0 built for MSWin32-x86-multi-thread (build 806) under
Win2K.
What I tried:
=========
- searched on Google
- looked at the Perl documentation locally available
- ran the script Open_Perl_IDE\PerlIDE.exe ( I don't seem able to reproduce
this problem with that debugger or maybe I don't know how to use it.
In advance sorry for the lengthy email. I'm new at using Perl and I have
never posted here so my goal is to hopefully provide as much information as
neccessary. I provided:
- a broken test script exhibiting a problem (along with the output)
- a fixed test script no longer exhibiting the problem (along with the
output)
- a quick description of my environment
- a list of what I tried
One could wonder why I'm posting here since I already have 'a fix'. Well, I
would like to understand why moving this specific line of code fixed the
problem. Specifically, I want to understand why it seems that a 'hash' can
be alternatively 'initialized' or 'uninitialized' in the broken script. It
just does not make sense to me with my limited knowledge.
In my example, assuming that %messages turned out to be a very big hash,
what would be the impact of rebuilding the @sortedmessages array every time
in the loop as I'm doing in the 'fixed' test script? Yes, I know that it
sounds like I want to optimize before I even know how to code. Here again,
it is just for my own 'edification'. Thanks for reading the rest of the mail
and teaching me along.
Note 1: the script is written for learning purpose only, you are welcome to
comment on it also.
Note 2: English is not my native language and I'm trying really hard to be
understood on this forum.
The test script exhibiting a problem:
=========================
use strict;
use warnings;
my %messages;
$messages{"C"}="333";
$messages{"B"}="222";
$messages{"A"}="111";
my @sortedmessages = map { { ($_ => $messages{$_}) } } sort keys %messages;
my $maxi=2;
my $message_element;
my $key = my $value;
foreach my $i (0...$maxi)
{
foreach $message_element (@sortedmessages)
{
($key, $value) = each %$message_element;
print ("key is $key and value is $value\n");
}
}
Output with the broken script:
=====================
key is A and value is 111
key is B and value is 222
key is C and value is 333
Use of uninitialized value in concatenation (.) or string at
D:\test.pl line 16.
Use of uninitialized value in concatenation (.) or string at
D:\test.pl line 16.
key is and value is
Use of uninitialized value in concatenation (.) or string at
D:\test.pl line 16.
Use of uninitialized value in concatenation (.) or string at
D:\test.pl line 16.
key is and value is
Use of uninitialized value in concatenation (.) or string at
D:\test.pl line 16.
Use of uninitialized value in concatenation (.) or string at
D:\test.pl line 16.
key is and value is
key is A and value is 111
key is B and value is 222
key is C and value is 333
The 'fixed' test script no longer exhibiting a problem
=====================================
use strict;
use warnings;
my %messages;
$messages{"C"}="333";
$messages{"B"}="222";
$messages{"A"}="111";
my $maxi=2;
my $message_element;
my $key = my $value;
foreach my $i (0...$maxi)
{
#line 7 from broken script was moved here and fixed the problem
my @sortedmessages = map { { ($_ => $messages{$_}) } } sort keys
%messages;
foreach $message_element (@sortedmessages)
{
($key, $value) = each %$message_element;
print ("key is $key and value is $value\n");
}
}
Output with fixed script:
=====================
key is A and value is 111
key is B and value is 222
key is C and value is 333
key is A and value is 111
key is B and value is 222
key is C and value is 333
key is A and value is 111
key is B and value is 222
key is C and value is 333
Current environment:
==============
ActiveState Perl v5.8.0 built for MSWin32-x86-multi-thread (build 806) under
Win2K.
What I tried:
=========
- searched on Google
- looked at the Perl documentation locally available
- ran the script Open_Perl_IDE\PerlIDE.exe ( I don't seem able to reproduce
this problem with that debugger or maybe I don't know how to use it.