Ken Foskey wrote:
I have a piece of code that I am assured works and I cannot see why it
would.  Code is supposed to force undefined, zero and all space to
numeric zero to stop printf being undefined.

foreach my $value (@array) {
    if( ! $value or $value = "      " ) {
-----------------------------^
Should be:

    if( ! $value or $value eq "      " ) {
---------------------------^^

        $value = 0;
    }
}

Will this actually work,

Yes, I suppose so.

or as I suspect do nothing because $value is a copy.

$value is not a copy, it's an alias.

The root cause of the problem is that the array is read from a file with
a simple substr

  $array[0] = substr( $_, 256, 6 );

Despite documented standards some values are simply filled with blanks.

Then rebuilt with a printf "%06d", $array[0]; and this causes a non
numeric warning.

C:\home>type test.pl
use strict;
use warnings;

my @array = ( undef, '', '      ' );

printf "%06d, %06d, %06d\n\n", @array;

foreach my $value (@array) {
    if( ! $value or $value eq "      " ) {
        $value = 0;
    }
}

printf "%06d, %06d, %06d\n", @array;

C:\home>perl test.pl
Use of uninitialized value in printf at test.pl line 6.
Argument "" isn't numeric in printf at test.pl line 6.
Argument "      " isn't numeric in printf at test.pl line 6.
000000, 000000, 000000

000000, 000000, 000000

C:\home>

So yes, it seems to 'work'.

Why didn't you test it yourself instead of asking?

Is there a 'better' way to do this.

Don't know.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to