Uninitialized value in concatenation

Uninitialized value in concatenation

am 08.06.2011 14:02:39 von Sayth Renshaw

Working through the exercises in Learning Perl. Chapter 3 exercise 2.

Basically have to look up a set of names and return an output list of
based on the numbers selected by the user. 1 would print fred etc. For
the longest time I had the below code but couldn't figure out the body
of the foreach loop so I reviewed the appendix at it supplied the
answer as print "$mynames[ $_ - 1 ]\n";

However I keep receiving the following error when attempting to use this script.

This is the code:

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @mynames = qw/fred betty barney dino wilma pebbles bamm-bamm/;
print " Enter a number from 1 to 7: ";
chomp(my @nums = );
foreach (@nums) {
print "$mynames[ $_ - 1 ]\n";
}

The error:

C:\MyPerl>perl ch3_2.pl
Enter a number from 1 to 7: 2
^Z
Use of uninitialized value $mynames in concatenation (.) or string at ch3_2.pl l
ine 11, line 1.
[ 2 -1 ]

C:\MyPerl>

Any idea where it comes from?

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

Re: Uninitialized value in concatenation

am 08.06.2011 14:12:06 von Cortello

On 08/06/2011 13:02, Sayth Renshaw wrote:
> Working through the exercises in Learning Perl. Chapter 3 exercise 2.
>
> Basically have to look up a set of names and return an output list of
> based on the numbers selected by the user. 1 would print fred etc. For
> the longest time I had the below code but couldn't figure out the body
> of the foreach loop so I reviewed the appendix at it supplied the
> answer as print "$mynames[ $_ - 1 ]\n";
>
> However I keep receiving the following error when attempting to use this script.
>
> This is the code:
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Data::Dumper;
>
> my @mynames = qw/fred betty barney dino wilma pebbles bamm-bamm/;
> print " Enter a number from 1 to 7: ";
> chomp(my @nums =);
> foreach (@nums) {
> print "$mynames[ $_ - 1 ]\n";
> }
>
> The error:
>
> C:\MyPerl>perl ch3_2.pl
> Enter a number from 1 to 7: 2
> ^Z
> Use of uninitialized value $mynames in concatenation (.) or string at ch3_2.pl l
> ine 11, line 1.
> [ 2 -1 ]
>
> C:\MyPerl>
>
> Any idea where it comes from?

Hello Sayth

The program you have published seems to work fine. Are you certain that
the print line inside the foreach loop is as you say?

Rob

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

Re: Uninitialized value in concatenation

am 08.06.2011 14:39:54 von Sayth Renshaw

> Hello Sayth
>
> The program you have published seems to work fine. Are you certain that
> the print line inside the foreach loop is as you say?
>
> Rob
>

This is copied direct from my editor.

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

my @mynames = qw/fred betty barney dino wilma pebbles bamm-bamm/;
print " Enter a number from 1 to 7: ";
chomp(my @nums = );
foreach (@nums) {
print "$mynames[ $_ - 1 ]\n";
}

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

Re: Uninitialized value in concatenation

am 08.06.2011 16:32:35 von Rob

On 08/06/2011 13:02, Sayth Renshaw wrote:
> Working through the exercises in Learning Perl. Chapter 3 exercise 2.
>
> Basically have to look up a set of names and return an output list of
> based on the numbers selected by the user. 1 would print fred etc. For
> the longest time I had the below code but couldn't figure out the body
> of the foreach loop so I reviewed the appendix at it supplied the
> answer as print "$mynames[ $_ - 1 ]\n";
>
> However I keep receiving the following error when attempting to use this script.
>
> This is the code:
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Data::Dumper;
>
> my @mynames = qw/fred betty barney dino wilma pebbles bamm-bamm/;
> print " Enter a number from 1 to 7: ";
> chomp(my @nums =);
> foreach (@nums) {
> print "$mynames[ $_ - 1 ]\n";
> }
>
> The error:
>
> C:\MyPerl>perl ch3_2.pl
> Enter a number from 1 to 7: 2
> ^Z
> Use of uninitialized value $mynames in concatenation (.) or string at ch3_2.pl l
> ine 11, line 1.
> [ 2 -1 ]
>
> C:\MyPerl>
>
> Any idea where it comes from?

That looks fine, but Perl is trying to interpolate $mynames (which
hasn't been initalised) into the string instead of indexing an element
of @mynames.

You may be running an old perl. Please check your version with

perl -v

and, in the mean time, fix your program by changing the print statement
to

print $mynames[$_-1], "\n";

HTH,

Rob

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

Re: Uninitialized value in concatenation

am 09.06.2011 02:33:22 von Sayth Renshaw

> That looks fine, but Perl is trying to interpolate $mynames (which
> hasn't been initalised) into the string instead of indexing an element
> of @mynames.
>
> You may be running an old perl. Please check your version with
>
> =A0perl -v

I have 5.12.3 on this PC, The strawberry perl release.

>
> and, in the mean time, fix your program by changing the print statement
> to
>
> =A0print $mynames[$_-1], "\n";
>
> HTH,
>
> Rob
>
Yes that got it all working. Regarding the actual $_ that as I read it
is the perl default.

So in the context of this program is it saying whatever the default is
in the foreach loop for $mynames -1 from it(to correct the index) and
proceed.

Sayth

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

Re: Uninitialized value in concatenation

am 09.06.2011 02:49:13 von Jim Gibson

On 6/8/11 Wed Jun 8, 2011 5:33 PM, "Sayth Renshaw"
scribbled:

>> That looks fine, but Perl is trying to interpolate $mynames (which
>> hasn't been initalised) into the string instead of indexing an element
>> of @mynames.
>>=20
>> You may be running an old perl. Please check your version with
>>=20
>> =A0perl -v
>=20
> I have 5.12.3 on this PC, The strawberry perl release.
>=20
>>=20
>> and, in the mean time, fix your program by changing the print statement
>> to
>>=20
>> =A0print $mynames[$_-1], "\n";
>>=20
>> HTH,
>>=20
>> Rob
>>=20
> Yes that got it all working. Regarding the actual $_ that as I read it
> is the perl default.

The $_ variable is the default variable for many Perl constructs, including
the input operator, for and while loops, several built-in functions such as
split, and the binding operator, etc.

>=20
> So in the context of this program is it saying whatever the default is
> in the foreach loop for $mynames -1 from it(to correct the index) and
> proceed.

"$mynames[$_-1]" is a double-quoted string that contains an array element.
The array element is indexed by the value of the scalar expression ($_-1).
Your perl is apparently not properly detecting that the string contains an
array element and attempting to evaluate the scalar variable $mynames
instead of the array @mynames.

$mynames[$_-1] in 'print $mynames[$_-1],"\n";' is an expression. The rules
for evaluating expressions and for interpolating into double-quoted strings
are different. The expression evaluation for $mynames[$_-1] is working
correctly.

Your Perl may have a bug, or something unknown is going on.



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