in reply to Re: Understanding variable scope in perl
in thread Understanding variable scope in perl

Regarding this kind of logic:
my $val1 = $next_cell_arr[0] || die "val1 is undefined!!\n";
You need to understand a subtle difference between things that evaluate to false, and things that are "undefined". In a nutshell:
use strict; my $val1; # this creates a named storage location for a scalar value, # but DOES NOT ASSIGN A VALUE -- its value is undefined. my $val2 = 0; # creates a named scalar and assigns a value, so it's d +efined # but the value will evaluate to FALSE in a # boolean sense my $val3 = ''; # similar to $val2: it's defined, it evaluates to false # in boolean tests, but in "reality", it's an # "empty string" (which $val2 is not) print "val3 is an empty string\n" if ( $val3 eq '' ); print "val2 is an empty string\n" if ( $val2 eq '' ); print "val1 is (like) an empty string\n" if ( $val1 eq ''); # this third test would generate a warning in "perl -w" my $i = 1; for ( $val1, $val2, $val3 ) { print "val$i evaluates to false\n" if ( ! $_ ); print "val$i is undefined\n" if ( ! defined( $_ )); $i++; }
Now, bear in mind that the "||" operator applies a boolean test: the expression that follows it is evaluated only if the expression that precedes it evaluates to false. Do you have empty strings or zeros in your matrix cells?

Based on the original statement of the problem, are you sure that the very first thing ever pushed onto your "@next_cell_arr" array is a defined value? Are you sure that the array is empty before you do the first of your three pushes from "@protein_matrix"?

Replies are listed 'Best First'.
Re^3: Understanding variable scope in perl
by BioBoy (Novice) on Sep 29, 2005 at 23:27 UTC
    The answer to your questions are yes and yes. Just before I push values from my '@protein_matrix' to my '@next_cell_arr' matrix, I have debug code that shows all the values of my '@protein_matrix', becaue I did some computations in it before hand. When I didn't have the '|| die' code in there, perl threw errors stating that I'm trying to use uninitialized values.