For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > April 2007 > uninitialized value in pattern match









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author uninitialized value in pattern match
guba@vi-anec.de

2007-04-18, 7:58 am

Hello,

I get the error message "use of uninitialized value in pattern
match ..." located
in the following expression that is working otherwise:

....
for (my $j = 0; $j <= $parameter_list_nr; $j++) {
if ($parameter_list[$j] =~ /parameter_1$/) {$parameter_1 =
$parameter_list[$j+1];
};

Could someone tell me how to correct this? Thank you!

Guenter

nobull67@gmail.com

2007-04-18, 7:58 am

On Apr 18, 10:07 am, "g...@vi-anec.de" <g...@vi-anec.de> wrote:
> Hello,
>
> I get the error message "use of uninitialized value in pattern
> match ..."


Actually that's a warning.

> located
> in the following expression that is working otherwise:


If it were an error then execution would have been aborted.

> ...
> for (my $j = 0; $j <= $parameter_list_nr; $j++) {
> if ($parameter_list[$j] =~ /parameter_1$/) {$parameter_1 =
> $parameter_list[$j+1];
>
> };
>
> Could someone tell me how to correct this?


Well, $parameter_list[$j] is undef.

So either @parameter_list contains at least an undef element or
$parameter_list_nr is larger than the number of elements in
@parameter_list.

I'm going to guess the second.

$parameter_list_nr = @parameter_list
if $parameter_list_nr > @parameter_list;

Since $parameter_list_nr wasn't isn't changing inside the loop you
don't need the ugly C-style for() loop. You can use the normal type of
Perl for loop (sometimes called a foreach loop).

for my $j (0 .. $parameter_list_nr -1 )

Note that if $parameter_list_nr is meant to be equal to the number of
elements in @parameter_list its a pointless waste of effort. Just do
the right thing instead:

for my $j (0 .. $#parameter_list )

guba@vi-anec.de

2007-04-19, 3:58 am

Hello!

Thank you for answering!
$parameter_list was defined; I used in the posting only the
line that caused the warning. The code has the following structure
("..." indicates only that there are more variables/lines of the same
type). But you were right with your guess: with
for my $j (0 .. $parameter_list_nr -1)
it worked. Thank you!

best regards
Guenter


######
use diagnostics;

my $TempParaTXT_folder = 'C:/ART/IM_Composing/TempParameterTXT/';
opendir(DIR, "$TempParaTXT_folder") || die "folder not found: $!";
@TempParaTXT = grep(/\.txt$/, readdir(DIR));

my $parameter_txt_name = $TempParaTXT[0];
my $parameter_txt_fullpath = $TempParaTXT_folder .
$parameter_txt_name;

open(A, "$parameter_txt_fullpath");
@parameter_list = <A>;
my $parameter_list_nr = @parameter_list;


my ($parameter_1, $parameter_2, ...);

for my $j (0 .. $parameter_list_nr -1) {
if ($parameter_list[$j] =~ /parameter_1$/) {$parameter_1 =
$parameter_list[$j+1]};
if ($parameter_list[$j] =~ /parameter_2$/) {$parameter_2 =
$parameter_list[$j+1]};
...
};
######

nobull67@gmail.com

2007-04-19, 6:58 pm

On Apr 19, 9:29 am, "g...@vi-anec.de" <g...@vi-anec.de> wrote:

> Thank you for answering!


You are wellcome.

> $parameter_list was defined;


I never suggested it wasn't. Indeed the the variable $parameter_list
has never been mentioned at all by anyone in this thread so far.

> I used in the posting only the line that caused the warning.


Please see the posting guidelines for comp.lang.perl.misc - they apply
equally here.

> The code has the following structure
> ("..." indicates only that there are more variables/lines of the same
> type).


Is your head hurting?

I hope so.

If you are looking at code where you feel the need to put in "..."
meaning "and the same thing over and over again with slight
differences" then you should find this experience painful.

In all but the most trivial programming languages we have loops and
arrays and maybe higher order things too. It is a measure of both a
programmer and the language how few times you need to repeat
yourself.

I sum it up with the maxim "if you're doing it three times you are
probably doing it wrong".

> But you were right with your guess: with
> for my $j (0 .. $parameter_list_nr -1)
> it worked.


What is the purpose (if any) of the $parameter_list_nr variable?

> ######
> use diagnostics;


The sooner you start using strict the less pain you will experience.

> my $TempParaTXT_folder = 'C:/ART/IM_Composing/TempParameterTXT/';
> opendir(DIR, "$TempParaTXT_folder") || die "folder not found: $!";


See FAQ: What's wrong with always quoting "$vars"?

> @TempParaTXT = grep(/\.txt$/, readdir(DIR));
>
> my $parameter_txt_name = $TempParaTXT[0];


What is the purpose of @TempParaTXT ? Why not simply:

my ($parameter_txt_name) = grep(/\.txt$/, readdir(DIR));

Also consider using File::Slurp.

> my $parameter_txt_fullpath = $TempParaTXT_folder .
> $parameter_txt_name;
>
> open(A, "$parameter_txt_fullpath");


You forgot the "or die".

Consider getting into the habit of using lexical file handles and the
3-arg open(). Bareword filehandles and the 2-arg open should not even
be _shown_ to beginners.

See FAQ: What's wrong with always quoting "$vars"?

Where are you getting all these bad habits?

open my $A, '<', $parameter_txt_fullpath or die "open
$parameter_txt_fullpath: $!";

> @parameter_list = <A>;


You forgot to declare @parameter_list. Perl would have spotted this
for you if you'd allowed it to.

> my $parameter_list_nr = @parameter_list;


Why to you feel the need to make a record of the original size of
@parameter_list ? The size never changes so you could just use the
current size everywhere and make your code both shorter and easier to
read.

> my ($parameter_1, $parameter_2, ...);


How can anyone look at that without realising that you should be
declaring some kind of array instead?

> for my $j (0 .. $parameter_list_nr -1) {
> if ($parameter_list[$j] =~ /parameter_1$/) {$parameter_1 =
> $parameter_list[$j+1]};
> if ($parameter_list[$j] =~ /parameter_2$/) {$parameter_2 =
> $parameter_list[$j+1]};
> ...};
>
> ######


There are much better ways to do this. A lot depends on what your
parameters really look like. Are really the word parameter followed by
an underscore and a number? Are the numbers consecutive?

nobull67@gmail.com

2007-04-24, 7:58 am

On Apr 20, 7:40 pm, "g...@vi-anec.de" <g...@vi-anec.de> wrote:
>
>
> Having the list of variable names I would appreciate
> a better way to initialize the variables with the
> values than my original proposal.


The "better way" is not to have a list of discrete named scalar
variables in the fist place but rather a single hash.

For details see FAQ: "How can I use a variable as a variable name?"

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com