Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > A small problem I can't quite work out.

Reply
Thread Tools

A small problem I can't quite work out.

 
 
sj
Guest
Posts: n/a
 
      03-19-2009
I'm making a program just for the joy of it, really, but I'm getting a
bug I just can't work out. Basically, when I run it, I get the message
'uninitiated value $whatever in pattern match (m//) at C:\wherever
line whatever'.
The problem is, they are initiated...I can post the code if it would
make this easier, but all variables are declared with 'my' not
'local'...I've tried putting them in different places in the code,
within loops or all at the begining etc...it used to work :/ maybe I
should just start again!
 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      03-19-2009
sj <(E-Mail Removed)> wrote:

> Subject: A small problem I can't quite work out.



Please put the subject of your article in the Subject of your article.

Subject: Use of uninitialized value

or some such.


> I'm making a program just for the joy of it, really, but I'm getting a
> bug I just can't work out. Basically, when I run it, I get the message
> 'uninitiated value $whatever in pattern match (m//) at C:\wherever
> line whatever'.



Please post the *actual text* of messages rather than paraphrase them.

Have you seen the Posting Guidelines that are posted here frequently?


> The problem is, they are initiated...



The word is "uninitialized", not uninitiated.


> I can post the code if it would
> make this easier,



It would not make it easier.

It would make it *possible*.


> but all variables are declared with 'my' not



"declaring a variable" and "defining a variable" are separate
and distinct concepts.

my() has to do with declaring variables.

Your message has to do with defining variables.


> 'local'...I've tried putting them in different places in the code,
> within loops or all at the begining etc...it used to work :/ maybe I
> should just start again!



We cannot repair code that we cannot see...


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
 
sj
Guest
Posts: n/a
 
      03-19-2009
Sorry, I'm completely new to both programming and usenet, I'll attempt
to pick it up as fast as possible.
I *think* it's the 'special' and 'checkinput' subs that are having the
problems...

#use strict;
#use warnings;

my $checked2;
my $flag = 1;
my $flag2 = 1;

while ($flag) {
print $flag;
my $usec;
if ($flag == 1) {
$usec = start();
} else {
$usec = restart();
}
$flag = $flag + 1;

my $use = checkinput ($usec);

writechat ($use);

printchatall ();

if ($use =~ /exit\n/i) {
$flag = 0;
}
}

sub start {
intro();
my $in = input();
$flag2 = 1;
return $in;
}

sub restart {
print "what would you like me to say?: \n";
my $in2 = input();
$flag2 = 1;
return $in2;
}

sub input {
my $input = <>;
return $input;
}

sub helprint {
open (HELP,"< help.txt") || die("can't find help file: $!");
while (<HELP>) {
print $_;
}
close HELP;
return 0;
}

sub checkinput {
while ($flag2 >= 1) {
my $checked2;
my $test2 = shift;
my $checked;
$checked = special($test2);
unless ($checked =~ /\n/i || $checked =~ /help\n/i || $checked =~ /
clear\n/i || $checked =~ /""/ || $checked =~ /exit\n/i) {
$flag2 = 0;
}
$flag2 = $flag2 + 1;
if ($flag2 >= 2) {
$checked2 = special($checked);
}
}
return $checked2;
}
sub special {

my $test = (shift);

if ($test =~ /^help\n$/i) {
helprint ();
my $newans = input();
return $newans;
} elsif ($test =~ /^exit\n$/i) {
$flag = 0;
return 0;
} elsif ($test =~ /^clear\n$/i) {
open (CLEAR,"> chatthing.txt") || die("can't clear: $!");
print CLEAR "";
my $newans1 = input();
return $newans1;
}else {
return $test;
}
return "/n"
}

sub intro {
open (INTRO,"< intro.txt") || die("can't find the intro: $!");
while (<INTRO>) {
print $_;
}
close INTRO;
}

sub writechat {
my $answer = shift;
open (CHATW,">> chatthing.txt") || die("can't open datafile: $!");
print CHATW $answer;
close CHATW;
}

sub printchatall {
open (CHATR,"< chatthing.txt") || die("can't read datafile: $!");
while (<CHATR>) {
print $_;
}
close CHATR;
}
 
Reply With Quote
 
sj
Guest
Posts: n/a
 
      03-19-2009
Okay, fixed it myself *is embarassed*
tried to delete the thread, but did it wrong.
Oh gosh, I'm a complete n00b...Never mind.
 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      03-19-2009
In article
<(E-Mail Removed)>, sj
<(E-Mail Removed)> wrote:

> Sorry, I'm completely new to both programming and usenet, I'll attempt
> to pick it up as fast as possible.
> I *think* it's the 'special' and 'checkinput' subs that are having the
> problems...


Thank you for making the effort to help us help you.

>
> #use strict;
> #use warnings;


use strict and warnings are very valuable tools. However, they don't
work if they are commented out. Some people here will not look at your
code unless both of these are present.

>
> my $checked2;
> my $flag = 1;
> my $flag2 = 1;
>
> while ($flag) {
> print $flag;
> my $usec;
> if ($flag == 1) {
> $usec = start();
> } else {
> $usec = restart();
> }
> $flag = $flag + 1;
>
> my $use = checkinput ($usec);
>
> writechat ($use);
>
> printchatall ();
>
> if ($use =~ /exit\n/i) {
> $flag = 0;
> }
> }
>
> sub start {
> intro();
> my $in = input();
> $flag2 = 1;
> return $in;
> }
>
> sub restart {
> print "what would you like me to say?: \n";
> my $in2 = input();
> $flag2 = 1;
> return $in2;
> }
>
> sub input {
> my $input = <>;
> return $input;
> }


This routine can be simplified to:

sub input {
return <>;
}

or even

sub input {
<>;
}

but you might want to remove the newline at the end of the input string:
sub input {
my $line = <>;
chomp($line);
return $line;
}

>
> sub helprint {
> open (HELP,"< help.txt") || die("can't find help file: $!");
> while (<HELP>) {
> print $_;
> }
> close HELP;
> return 0;
> }
>
> sub checkinput {
> while ($flag2 >= 1) {
> my $checked2;
> my $test2 = shift;


This statement is inside a while loop. Therefore, it can be executed
more than once, depending upon the logic involved in setting the value
of $flag2. While your program logic is convoluted, I do believe that
this loop will iterate more than once unless the user has entered a
special string (empty line, 'help', 'clear', etc.). You have called
this routine with one argument. The first time through the loop, the
argument (the user input) will be assigned to $test2 and shifted off
the @_ array. However, the second time through the loop, the @_ array
will be empty and $test2 will be undefined. This is the source of your
"Uninitialized" warnings.

You need to rethink the logic behind your user input processing logic.


> my $checked;
> $checked = special($test2);
> unless ($checked =~ /\n/i || $checked =~ /help\n/i || $checked =~ /
> clear\n/i || $checked =~ /""/ || $checked =~ /exit\n/i) {


$checked =~ /\n/i tests for the presence of a newline character in
the user input. Since such a character will always be present, this
test will never succeed. If you are trying to test for an empty line,
you need to anchor the R.E. at the beginning of the string:

$checked =~ /^\n/;

(there is no need to test case-insentively here).

It is better to use the regular expression meta-character '$' to test
for end of the string, and if you chomp the input, then this becomes:

unless ($checked =~ /^$/;

You are checking user input for special values both here and below. You
should only be doing this in one place. Checking just once will
simplify and improve your program logic.

> $flag2 = 0;
> }
> $flag2 = $flag2 + 1;
> if ($flag2 >= 2) {
> $checked2 = special($checked);
> }
> }
> return $checked2;
> }
> sub special {
>
> my $test = (shift);
>
> if ($test =~ /^help\n$/i) {
> helprint ();
> my $newans = input();
> return $newans;
> } elsif ($test =~ /^exit\n$/i) {
> $flag = 0;
> return 0;
> } elsif ($test =~ /^clear\n$/i) {
> open (CLEAR,"> chatthing.txt") || die("can't clear: $!");


You are using the string 'chatthing.txt' three times in your program.
You should put this string in a variable and use the variable
throughout your program.

Using the three-argument version of open and lexically-scoped (my)
variables for file handles is recommended:

open( my $clear, '>', $outfile ) or die("Can't open $outfile: $!");

> print CLEAR "";


print $clear "";

however, printing an empty string is a no-operation.

> my $newans1 = input();
> return $newans1;
> }else {
> return $test;
> }
> return "/n"
> }
>
> sub intro {
> open (INTRO,"< intro.txt") || die("can't find the intro: $!");
> while (<INTRO>) {
> print $_;
> }
> close INTRO;
> }
>
> sub writechat {
> my $answer = shift;
> open (CHATW,">> chatthing.txt") || die("can't open datafile: $!");
> print CHATW $answer;
> close CHATW;
> }
>
> sub printchatall {
> open (CHATR,"< chatthing.txt") || die("can't read datafile: $!");
> while (<CHATR>) {
> print $_;
> }
> close CHATR;
> }


--
Jim Gibson
 
Reply With Quote
 
sj
Guest
Posts: n/a
 
      03-19-2009
On Mar 19, 9:05*pm, Jim Gibson <(E-Mail Removed)> wrote:
> In article
> <(E-Mail Removed)>, sj
>
> <(E-Mail Removed)> wrote:
> > Sorry, I'm completely new to both programming and usenet, I'll attempt
> > to pick it up as fast as possible.
> > I *think* it's the 'special' and 'checkinput' subs that are having the
> > problems...

>
> Thank you for making the effort to help us help you.
>
>
>
> > #use strict;
> > #use warnings;

>
> use strict and warnings are very valuable tools. However, they don't
> work if they are commented out. Some people here will not look at your
> code unless both of these are present.
>
>
>
>
>
> > my $checked2;
> > my $flag = 1;
> > my $flag2 = 1;

>
> > while ($flag) {
> > * print $flag;
> > * my $usec;
> > if ($flag == 1) {
> > * $usec = start();
> > } else {
> > * $usec = restart();
> > }
> > $flag = $flag + 1;

>
> > my $use = checkinput ($usec);

>
> > writechat ($use);

>
> > printchatall ();

>
> > if ($use =~ /exit\n/i) {
> > $flag = 0;
> > }
> > }

>
> > sub start {
> > * intro();
> > * my $in = input();
> > * $flag2 = 1;
> > * return $in;
> > }

>
> > sub restart {
> > * print "what would you like me to say?: \n";
> > * my $in2 = input();
> > * $flag2 = 1;
> > * return $in2;
> > }

>
> > sub input {
> > * my $input = <>;
> > * return $input;
> > }

>
> This routine can be simplified to:
>
> * sub input {
> * * return <>;
> * }
>
> or even
>
> * sub input {
> * * <>;
> * }
>
> but you might want to remove the newline at the end of the input string:
> * sub input {
> * * my $line = <>;
> * * chomp($line);
> * * return $line;
> * }
>
>
>
> > sub helprint {
> > * open (HELP,"< help.txt") || die("can't find help file: $!");
> > * while (<HELP>) {
> > * * print $_;
> > * }
> > * close HELP;
> > * return 0;
> > }

>
> > sub checkinput {
> > * while ($flag2 >= 1) {
> > * * my $checked2;
> > * * my $test2 = shift;

>
> This statement is inside a while loop. Therefore, it can be executed
> more than once, depending upon the logic involved in setting the value
> of $flag2. While your program logic is convoluted, I do believe that
> this loop will iterate more than once unless the user has entered a
> special string (empty line, 'help', 'clear', etc.). You have called
> this routine with one argument. The first time through the loop, the
> argument (the user input) will be assigned to $test2 and shifted off
> the @_ array. However, the second time through the loop, the @_ array
> will be empty and $test2 will be undefined. This is the source of your
> "Uninitialized" warnings.
>
> You need to rethink the logic behind your user input processing logic.
>
> > * * my $checked;
> > * * $checked = special($test2);
> > * * unless ($checked =~ /\n/i || $checked =~ /help\n/i || $checked =~ /
> > clear\n/i || $checked =~ /""/ || $checked =~ /exit\n/i) {

>
> * $checked =~ /\n/i tests for the presence of a newline character in
> the user input. Since such a character will always be present, this
> test will never succeed. If you are trying to test for an empty line,
> you need to anchor the R.E. at the beginning of the string:
>
> * $checked =~ /^\n/;
>
> (there is no need to test case-insentively here).
>
> It is better to use the regular expression meta-character '$' to test
> for end of the string, and if you chomp the input, then this becomes:
>
> * unless ($checked =~ /^$/;
>
> You are checking user input for special values both here and below. You
> should only be doing this in one place. Checking just once will
> simplify and improve your program logic.
>
>
>
> > * * * $flag2 = 0;
> > * * }
> > * * $flag2 = $flag2 + 1;
> > * * if ($flag2 >= 2) {
> > * * * $checked2 = special($checked);
> > * * }
> > * }
> > * return $checked2;
> > }
> > sub special {

>
> > * my $test = (shift);

>
> > * if ($test =~ /^help\n$/i) {
> > * * helprint ();
> > * * my $newans = input();
> > * * return $newans;
> > * } elsif ($test =~ /^exit\n$/i) {
> > * * $flag = 0;
> > * * return 0;
> > * } elsif ($test =~ /^clear\n$/i) {
> > * * open (CLEAR,"> chatthing.txt") || die("can't clear: $!");

>
> You are using the string 'chatthing.txt' three times in your program.
> You should put this string in a variable and use the variable
> throughout your program.
>
> Using the three-argument version of open and lexically-scoped (my)
> variables for file handles is recommended:
>
> * open( my $clear, '>', $outfile ) or die("Can't open $outfile: $!");
>
> > * * print CLEAR "";

>
> * print $clear "";
>
> however, printing an empty string is a no-operation.
>
>
>
> > * * my $newans1 = input();
> > * * return $newans1;
> > * }else {
> > * * return $test;
> > * }
> > * return "/n"
> > }

>
> > sub intro {
> > * open (INTRO,"< intro.txt") || die("can't find the intro: $!");
> > * while (<INTRO>) {
> > * * print $_;
> > * }
> > * close INTRO;
> > }

>
> > sub writechat {
> > * my $answer = shift;
> > * open (CHATW,">> chatthing.txt") || die("can't open datafile: $!");
> > * print CHATW $answer;
> > * close CHATW;
> > }

>
> > sub printchatall {
> > * open (CHATR,"< chatthing.txt") || die("can't read datafile: $!");
> > * while (<CHATR>) {
> > * * print $_;
> > * }
> > * close CHATR;
> > }

>
> --
> Jim Gibson


Cheers, that really helps a lot! I'll keep all that in mind. The
strict and warnings were commented out because I was trying to see
what difference it makes, I only just did it and forgot to undo it
before I posted. I'll make notes of all of this
 
Reply With Quote
 
Martien Verbruggen
Guest
Posts: n/a
 
      03-19-2009
On Thu, 19 Mar 2009 14:05:18 -0700,
Jim Gibson <(E-Mail Removed)> wrote:
> In article
><(E-Mail Removed)>, sj
><(E-Mail Removed)> wrote:
>
>>
>> sub input {
>> my $input = <>;
>> return $input;
>> }

>
> This routine can be simplified to:
>
> sub input {
> return <>;
> }


Except that the original always uses <> in a scalar context, while yours
uses <> in whatever context the input subroutine was used in.

Martien
--
|
Martien Verbruggen | Since light travels faster than sound,
http://www.velocityreviews.com/forums/(E-Mail Removed) | is that why some people appear bright
| until you hear them speak?
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      03-19-2009
On Thu, 19 Mar 2009 14:31:05 -0700, sj wrote:

> Cheers, that really helps a lot! I'll keep all that in mind. The strict
> and warnings were commented out because I was trying to see what
> difference it makes, I only just did it and forgot to undo it before I
> posted. I'll make notes of all of this


Keep in mind that people are really sensitive to such errors. Post the
exact code, with use strict and use warnings and the exact output from
the exact code you posted. Doing so will tremendously increase the
response from this group.

M4
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      03-19-2009
>>>>> "JG" == Jim Gibson <(E-Mail Removed)> writes:

>> sub input {
>> my $input = <>;
>> return $input;
>> }


JG> This routine can be simplified to:

JG> sub input {
JG> return <>;
JG> }

JG> or even

JG> sub input {
JG> <>;
JG> }

not exactly. his will always return 1 line but yours could slurp in the
whole <> until eof if called in list context. but there is no need for
this sub to begin with.

JG> but you might want to remove the newline at the end of the input string:
JG> sub input {
JG> my $line = <>;
JG> chomp($line);
JG> return $line;
JG> }

now it becomes a slightly more useful sub but not really if it is only
called in 1 place.


JG> You need to rethink the logic behind your user input processing logic.

the OP is new at coding and stuck in the old way of using flags for
state. it will take some real world experience and time for him to
unlearn this.

JG> print $clear "";

JG> however, printing an empty string is a no-operation.

actually that would print "" to the handle in $clear!

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      03-19-2009
sj <(E-Mail Removed)> wrote:
> On Mar 19, 9:05*pm, Jim Gibson <(E-Mail Removed)> wrote:
>> In article
>> <(E-Mail Removed)>, sj
>>
>> <(E-Mail Removed)> wrote:
>> > Sorry, I'm completely new to both programming and usenet, I'll attempt
>> > to pick it up as fast as possible.



[ snip 200 lines ]


> Cheers, that really helps a lot! I'll keep all that in mind.



When composing a followup, quote only enough text to establish the
context for the comments that you will add.

Quoting 200 lines and adding 5 lines is seen as impolite.

Have you seen the Posting Guidelines that are posted here frequently?


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange consumption CPU for a quite small program Guillaume Ebuprofen Ruby 1 10-22-2010 01:11 PM
Big cat really quite small Douglas MacDonald-Springthorpe Digital Photography 1 05-15-2008 08:08 AM
Writing an int to a file, not quite sure how buffers work. mellyshum123@yahoo.ca C Programming 30 11-27-2006 04:21 PM
Can't quite get the Membership/Roles configured to work on SQL 2005 Jack ASP .Net 0 08-13-2006 09:43 AM
C# Databinder.Eval didn't work quite right... =?Utf-8?B?RXNraW1v?= ASP .Net 3 11-07-2004 06:18 PM



Advertisments