in reply to Use of uninitialized value within
I am unable to duplicate your problem with my perl v5.16.3. I can suggest several improvements to your code.
Always use strict and warnings.
Use of printf with only a format is confusing. Use print
Your math is unnecessarily complicated.
use strict;
use warnings;
use POSIX qw(floor);
my @PC1=(58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17,9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7);
my @parr=('0','M','0','A','0','I','0','L');
print $parr[floor( $PC1[$_]/8 )], "\n" for (0..63);
My use of 'floor' is not strictly necessary in this case, but is recommended for the reason explained in int.
Re^2: Use of uninitialized value within
by AnomalousMonk (Archbishop) on Feb 03, 2015 at 17:51 UTC
|
I am unable to duplicate your problem with my perl v5.16.3.
I can duplicate the problem with both Strawberry 5.14.4.1 and ActiveState 5.8.9, both under Win32.
c:\@Work\Perl>perl -wMstrict -le
"use POSIX qw(floor);
;;
print 'perl version ', $];
;;
my @PC1=(58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17, 9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7);
;;
my @parr=('0','M','0','A','0','I','0','L');
printf $parr[floor( $PC1[$_]/8 )] for (0..63);
"
perl version 5.014004
Use of uninitialized value in printf at -e line 1.
L0I0A0M0L0I0A0M0L0I0A0M0L0I0A0ML0I0A0M0L0I0A0M0L0I0A0M0L0I0A0M0
As in the OPed code, the problem arises from the element of 64 in the @PC1 array producing an index of 8 into the @parr array, the highest valid index of which is 7.
Give a man a fish: <%-(-(-(-<
| [reply] [d/l] [select] |
Re^2: Use of uninitialized value within
by AnomalousMonk (Archbishop) on Feb 04, 2015 at 00:52 UTC
|
Your math is unnecessarily complicated.
One could further simplify the math by taking advantage of the fact that the [ ] subscript operator does its own 'floor' operation by just throwing away the fractional part of the (inherently) double number:
c:\@Work\Perl>perl -wMstrict -le
"use POSIX qw(floor);
;;
print 'perl version ', $];
;;
my @PC1=(58,50,42,34,26,18,10,2,
60,52,44,36,28,20,12,4,
62,54,46,38,30,22,14,6,
64,56,48,40,32,24,16,8,
57,49,41,33,25,17, 9,1,
59,51,43,35,27,19,11,3,
61,53,45,37,29,21,13,5,
63,55,47,39,31,23,15,7);
;;
my @parr = qw(0 M 0 A 0 I 0 L);
printf $parr[floor( $PC1[$_]/8 )] for (0..63);
print '';
printf $parr[ $_ / 8 ] for @PC1;
"
perl version 5.014004
Use of uninitialized value in printf at -e line 1.
L0I0A0M0L0I0A0M0L0I0A0M0L0I0A0ML0I0A0M0L0I0A0M0L0I0A0M0L0I0A0M0
Use of uninitialized value in printf at -e line 1.
L0I0A0M0L0I0A0M0L0I0A0M0L0I0A0ML0I0A0M0L0I0A0M0L0I0A0M0L0I0A0M0
(Of course, this doesn't address the root problem of generating an index that's out of range!)
Give a man a fish: <%-(-(-(-<
| [reply] [d/l] [select] |
Re^2: Use of uninitialized value within
by GrandFather (Saint) on Feb 03, 2015 at 19:47 UTC
|
You don't see the warning because you didn't run the script with warnings turned on. Most likely the OP used -w on the command line to achieve that.
Running the updated version of the script that you provided does show the warning. I guess you posted the code without running it?
Perl is the programming world's equivalent of English
| [reply] |
Re^2: Use of uninitialized value within
by BillKSmith (Monsignor) on Feb 03, 2015 at 23:06 UTC
|
Sorry guys, I missed the message when it scrolled off the screen.
| [reply] |
|
|