in reply to A better way than ||

Thank you so much, everybody :)

My apologies for not making some things clearer in my haste to simplify the question.

In my code above, the array @weights is pre-defined so I should not have that line to assign each token "0" because it can't possibly be undefined - this enlightenment came after reading the wonderful feedback that I've got from your replies.

The other array @digits is the result of an input from a web form. Specifically, it's an identification number of the shape 9525423E. @digits is gotten using a split:

# $input is received from a web submission, # assigned a value here for simplification $input = '9525423E'; @digits = split //, $input; my $letter = uc(pop @digits);
So @digits should contain 7 numeric characters after the pop. But users may enter an invalid identification number, e.g one that contains only 6 digits instead of 7. So the extra code is need to assign each individual element of @digits a value to prevent the "Use of uninitialized value..." warning.

Cheers!

Replies are listed 'Best First'.
Re^2: A better way than ||
by holli (Abbot) on Jan 01, 2006 at 10:55 UTC
    it's an identification number of the shape 9525423E. @digits is gotten using a split:
    I think in this case you're better of using a regex, so you can verify the input split more conveniently:
    my $input = '9525429E'; if ( $input =~ /^([0-9]{7})([A-Z])$/ ) { my @digits = split //, $1; print "@digits, $2"; } else { print "invalid input"; }


    holli, /regexed monk/

      In principle, that is the approach, but I’d execute it a little differently since we’re not writing Pascal:

      my ( $digits, $letter ) = $input =~ m{ ^ (\d{7}) ([A-Z]) $ }x; if( not defined $digits ) { print "Invalid input: $input\n"; return; # or last, or exit, depending on what sort of block we're in } # rest of code such as split //, $digits follows here

      That avoids imposing another level of indentation upon the code in the normal flow of execution, and it keeps the condition check and its error handling close together.

      Makeshifts last the longest.