Lost here.. Use of uninitialized value in print?

Lost here.. Use of uninitialized value in print?

am 04.04.2005 12:41:57 von five421

Hello everyone,

I am new to Perl and am trying to code a simple program that will read a
list of files from a text file then present the user with some options
regarding those files (hope that makes some sense). Unfortunately after
much searching/reading on the web and playing around with the code I
have come up with so far I am stuck with the same warning (Use of
uninitialized value in print).
The code I have come up with is below

#!perl -w

# File Name = aaaa.pl
# Date Monday, April 04, 2005

@files1to5 = ();
$list1 = "";
open (TEXT, "list.txt") or die "Error. No such file\n";
@rawtext=;
close(TEXT);
print "\n";
$list1 = @rawtext;
@files1to5 = split (/\$/, $list1);
print "Please enter the file you wish to select..\n\n";
chomp($selectitem = );
if($selectitem == 1)
{
print "\n";
print "You selected item 1,\n";
print $files1to5[0];
$item = 1;
}
elsif($selectitem == 2)
{
print "\n";
print "You selected item 2,\n";
print $files1to5[1];
$item = 2;
}
elsif($selectitem == 3)
{
print "\n";
print "You selected item 3,\n";
print $files1to5[2];
$item = 3;
}
elsif($selectitem == 4)
{
print "\n";
print "You selected item 4,\n";
print $files1to5[3];
$item = 4;
}
elsif($selectitem == 5)
{
print "\n";
print "You selected item 5,\n";
print $files1to5[4];
$item = 5;
}
else
{
print "\n";
print "Invalid item selection! No such item\n";
}


If I select item 1, the code fails on the line

print $files1to5[0];

with the output


D:\My Documents\Perl>aaaa.pl

Please enter the file you wish to select..

1

You selected item 1,
Use of uninitialized value in print at D:\My Documents\Perl\aaaa.pl line
20, line 1.


and similarly for the other possible selections. I'm sure this is
something glaringly simple that i'm overlooking, yet I still cannot work
it out. Any assistance would be much appreciated.

Thanks

--

five421@fastmail.fm


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Re: Lost here.. Use of uninitialized value in print?

am 04.04.2005 13:23:35 von Owen

On Mon, 04 Apr 2005 20:41:57 +1000
five421@fastmail.fm wrote:

>
>
> I am new to Perl and am trying to code a simple program that will read a
> list of files from a text file then present the user with some options
> regarding those files (hope that makes some sense). Unfortunately after
> much searching/reading on the web and playing around with the code I
> have come up with so far I am stuck with the same warning (Use of
> uninitialized value in print).
> The code I have come up with is below
>
> #!perl -w
>
> # File Name = aaaa.pl
> # Date Monday, April 04, 2005
>
> @files1to5 = ();
> $list1 = "";
> open (TEXT, "list.txt") or die "Error. No such file\n";
> @rawtext=;
> close(TEXT);
> print "\n";
> $list1 = @rawtext;
> @files1to5 = split (/\$/, $list1);

Something from this is empty giving you the uninitialized error


You have a few lines of list.txt we could have a look at?


Owen

ps. you might also want to rewrite the script with

use strict;

You may get some additional help from that.

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Re: Lost here.. Use of uninitialized value in print?

am 04.04.2005 13:42:36 von five421

Hi,

Sorry, I should have included the text file in my original message. It
simply contains at this stage

$File1.txt$
$File2.txt$
$File3.txt$
$File4.txt$
$File5.txt$

Using use strict; I end up with a range of warnings, I don't know how
relevant they are here. Thanks again.


D:\My Documents\Perl>aaaa.pl
Global symbol "@files1to5" requires explicit package name at D:\My
Documents\Per
l\aaaa.pl line 7.
Global symbol "$list1" requires explicit package name at D:\My
Documents\Perl\aa
aa.pl line 8.
Global symbol "@rawtext" requires explicit package name at D:\My
Documents\Perl\
aaaa.pl line 10.
Global symbol "$list1" requires explicit package name at D:\My
Documents\Perl\aa
aa.pl line 13.
Global symbol "@rawtext" requires explicit package name at D:\My
Documents\Perl\
aaaa.pl line 13.
Global symbol "@files1to5" requires explicit package name at D:\My
Documents\Per
l\aaaa.pl line 14.
Global symbol "$list1" requires explicit package name at D:\My
Documents\Perl\aa
aa.pl line 14.
Global symbol "$selectitem" requires explicit package name at D:\My
Documents\Pe
rl\aaaa.pl line 16.
Global symbol "$selectitem" requires explicit package name at D:\My
Documents\Pe
rl\aaaa.pl line 17.
Global symbol "@files1to5" requires explicit package name at D:\My
Documents\Per
l\aaaa.pl line 21.
Global symbol "$item" requires explicit package name at D:\My
Documents\Perl\aaa
a.pl line 22.
Global symbol "$selectitem" requires explicit package name at D:\My
Documents\Pe
rl\aaaa.pl line 24.
Global symbol "@files1to5" requires explicit package name at D:\My
Documents\Per
l\aaaa.pl line 28.
Global symbol "$item" requires explicit package name at D:\My
Documents\Perl\aaa
a.pl line 29.
Global symbol "$selectitem" requires explicit package name at D:\My
Documents\Pe
rl\aaaa.pl line 31.
Global symbol "@files1to5" requires explicit package name at D:\My
Documents\Per
l\aaaa.pl line 35.
Global symbol "$item" requires explicit package name at D:\My
Documents\Perl\aaa
a.pl line 36.
Global symbol "$selectitem" requires explicit package name at D:\My
Documents\Pe
rl\aaaa.pl line 38.
Global symbol "@files1to5" requires explicit package name at D:\My
Documents\Per
l\aaaa.pl line 42.
Global symbol "$item" requires explicit package name at D:\My
Documents\Perl\aaa
a.pl line 43.
Global symbol "$selectitem" requires explicit package name at D:\My
Documents\Pe
rl\aaaa.pl line 45.
Global symbol "@files1to5" requires explicit package name at D:\My
Documents\Per
l\aaaa.pl line 49.
Global symbol "$item" requires explicit package name at D:\My
Documents\Perl\aaa
a.pl line 50.
Execution of H:\My Documents\Perl\aaaa.pl aborted due to compilation
errors.
--

five421@fastmail.fm


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Re: Lost here.. Use of uninitialized value in print?

am 04.04.2005 14:05:20 von Felix Geerinckx

On 04/04/2005, five421@fastmail.fm wrote:

> open (TEXT, "list.txt") or die "Error. No such file\n";
> @rawtext=;
> close(TEXT);

According to your own follow-up, @rawtext will have the following
content:
$rawtext[0] = '$file1.txt$' . "\n";
$rawtext[1] = '$file2.txt$' . "\n";
etc ...

> $list1 = @rawtext;

@rawtext is evaluated in scalar context (because of the scalar lvalue).
This gives the number of elements in @rawtext (or equivalent, the
number of lines in your file) - this is an integer

> @files1to5 = split (/\$/, $list1);

@files1to5 will have one element, $files1to5[0], containing the number
of lines in your file - this is probably not what you want...

I would do it like this:

#! perl
use warnings;
use strict;

my $infile = 'list.txt';
open (TEXT, $infile) or die "$infile: $!";
chomp(my @data = map { /^\$(.*)\$$/ } );
close(TEXT);

my $prompt =
"\nPlease enter the file you wish to select (q to quit): ";
print $prompt;
while ( chomp(my $selecteditem = )) {;
last if $selecteditem eq 'q';
if ($selecteditem =~ /^(1|2|3|4|5)$/) {
print "You selected item $1, \n$data[$1-1]\n";
} else {
print "Invalid input '$selecteditem'\n";
}
print $prompt;
}

(And you won't need the map {...} stuff if your input file doesn't
contain the $ before and after the filenames.)

--
felix

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Re: Lost here.. Use of uninitialized value in print?

am 04.04.2005 14:13:03 von Offer Kaye

On Apr 4, 2005 2:42 PM, five421@fastmail.fm wrote:
>
> Using use strict; I end up with a range of warnings, I don't know how
> relevant they are here. Thanks again.
>

Those aren't warnings, they are errors - your program is not compiling
correctly. You need to place a "my" in front of your variables when
you declare them (not when you use them) when you "use strict".

Originally, your problem is here:
$list1 = @rawtext;
This simply puts the number of items in the array @rawtext (i.e. the
number of lines of the input file) into the scalarvariable $list1.
What were you trying to do?

--
Offer Kaye

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Re: Lost here.. Use of uninitialized value in print?

am 04.04.2005 14:23:46 von Owen

On Mon, 04 Apr 2005 21:42:36 +1000
five421@fastmail.fm wrote:


> Sorry, I should have included the text file in my original message. It
> simply contains at this stage
>
> $File1.txt$
> $File2.txt$
> $File3.txt$
> $File4.txt$
> $File5.txt$


well, your line,
$list1 = @rawtext; ( using the above as list.txt) means

$list1 is the number of lines in @rawtext which is 5.

Then you want to go and split that value, 5 on non existant $ delimiters.


So the next question is "What are you expecting from your split operation?






> D:\My Documents\Perl>aaaa.pl
> Global symbol "@files1to5" requires explicit package name at D:\My
> Documents\Per
> l\aaaa.pl line 7.
> Global symbol "$list1" requires explicit package name at D:\My
> Documents\Perl\aa
> aa.pl line 8.



You actually need to explore the usage of strict. It sort of makes global or local variables and every variable must be so assigned by using 'my' for the most part.

Have a look at http://www.perl.com/pub/a/2001/01/begperl6.html#use%20strict as a starter.

However if you don't quite understand it, not using it will not be the end of the world.




Owen




--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Re: Lost here.. Use of uninitialized value in print?

am 04.04.2005 15:31:21 von five421

Hi,

> well, your line,
> $list1 = @rawtext; ( using the above as list.txt) means
>
> $list1 is the number of lines in @rawtext which is 5.
>
> Then you want to go and split that value, 5 on non existant $ delimiters.
>
> So the next question is "What are you expecting from your split operation?

Ughhh, now im even more confused, sorry. Eventually the text file will
have a range of other data in it, which will not be selected using the
split in the code, when I first noticed this error I took that text out.
What i want the split to do is select only the text in between the $,
then with the rest of the code print to screen the name of the text file
selected.
Are you saying the line of code you pointed out replaces the contents of
the array with "5" & as a result there are no $ deliminiters to split
with (please excuse my newbieness here). Now I think about it that seems
to make sense as it's a scalar variable! If so, what should be the
format of the next line, taking into account the rest of the code?

@files1to5 = split (/\$/, $list1);

The format of a split as I understand it is

@array = split / expression to split at /, string to be split;

So I guess if I could split my array that would be simpler but I don't
believe that's possible.


> > D:\My Documents\Perl>aaaa.pl
> > Global symbol "@files1to5" requires explicit package name at D:\My
> > Documents\Per
> > l\aaaa.pl line 7.
> > Global symbol "$list1" requires explicit package name at D:\My
> > Documents\Perl\aa
> > aa.pl line 8.
>
> You actually need to explore the usage of strict. It sort of makes global or local variables and every variable must be so assigned by using 'my' for the most part.
>
> Have a look at http://www.perl.com/pub/a/2001/01/begperl6.html#use%20strict as a starter.
>
> However if you don't quite understand it, not using it will not be the end of the world.

Thanks for that link. Ive bookmarked it & will read tomorrow morning.
It's difficult for me to understand most things so late at night :)
--

five421@fastmail.fm


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org