On Aug 20, 10:47 am, [EMAIL PROTECTED] (Ken Foskey) wrote:
> I have a piece of code that I am assured works
Assured by whom?
> and I cannot see why it would.
It doesn't.
> 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 = " " ) {
The first part of this if statement is "if $value is not true". The
second part, however, is "assign $value to a string of six spaces".
> $value = 0;
> }
Since $value = " " will always return a true value, this code will
change EVERY value of @array to be 0.
Change
$value = " "
to
$value !~ /\S/
so that it instead says "$value does not contain any non-whitespace
characters"
>
> }
>
> Will this actually work, or as I suspect do nothing because $value is a
> copy.
It doesn't work, but not for the reason you suspect. $value is not a
copy of the elements in @array. It is an alias. Observe:
my @array = (1, 2, 3);
for my $elem (@array) {
$elem = $elem * 2;
}
print "@array\n";
That code prints "2 4 6", because $elem is an alias to each element of
the array, and changes to $elem therefore affect the elements of
@array.
> 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.
I don't understand this. As opposed to what? Assuming the string is
more than 362 characters long, what are you thinking *should* be in
those six positions?
> Then rebuilt with a printf "%06d", $array[0]; and this causes a non
> numeric warning.
>
> Is there a 'better' way to do this.
Well, if you're sure you know what you're doing, you could just turn
the warning off in the specific scope where you don't want it. I
mean, it's a warning. If you're comfortable with what you're doing
(printing a string of spaces as though it was a number), then there's
no reason to care about the warning. Just disable it as locally as
possible:
$array[0] = substr( $_, 256, 6 );
{
no warnings 'numeric';
printf "%06d", $array[0];
}
Paul Lalli
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/