Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

undefined elements in array

by Ntav (Sexton)
on Sep 09, 2001 at 15:43 UTC ( #111269=perlquestion: print w/replies, xml ) Need Help??
Ntav has asked for the wisdom of the Perl Monks concerning the following question:

problem: I'm adding elements to an array according to positions defined in a lookup table, so when it comes time to print the array some elements are undefined.(this is the correct behaviour - not to be "fixed")
current solution: when printing, I test:
if($array[$i][$j] == 0){ $array[$i][$j] = 0;}
and though this works (i.e. when I print I don't try and print an uninitialized variable) if I execute with warnings on, perl complains.
question: So, learned monks, what is the smart solution?
Not Another Perl Hacker
In fact I'm more used to declarative programming so the above code looks rather odd to me!

Replies are listed 'Best First'.
Re: undefined elements in array
by jlongino (Parson) on Sep 09, 2001 at 15:59 UTC
    Why not use a hash? This is where they shine:

    ## I don't know how you're getting values for $i, $j, $value $hash{$i}{$j} = $value;
    Update2: To print:
    foreach $i (keys %hash) { foreach $j (keys %{ $hash{$i} }) { print $i, $j, $hash{$i}{$j}, "\n"; } }
    Hope this works for you.

    Update1: If you insist on arrays, however, this will also work (but is more work):

    push @array, "$i:$j:$value"; ## to print: foreach (@array) { print join (' ', split ":"); }
    @a=split??,'just lose the ego and get involved!';
    for(split??,'afqtw{|~'){print $a[ord($_)-97]}
Re: undefined elements in array
by demerphq (Chancellor) on Sep 09, 2001 at 19:38 UTC
    Well similer to some of the others you might do one of the below, depending if you like map or foreach or subs.

    Oh, and something to remember on this level '||' returns the last true value evaluated, or if there aren't any the last value evaluated whatever it is, this is useful for applying defaults to variables, or for creating fallbacks in certain situations.

    For instance if your array was being populated by a function that returned a numeric value if legal input was provided and undef if not you could convert the undefs nicely to 0 by this:

    my $var=func($param) || 0;
    You might not want to do this if func() returns strings because "" would turn into 0, but in some situations its an acceptable approach.

    More useful is that the assignment version of most of the binary operators (+= -= etc) dont throw warnings if the var is undef to start with, even though the binary versions do. So we only get 1 warning from the below, even though on a conceptual level they are identical.

    my ($x,$y); $x=$x+1; # throws warning $y+=1; # no warning
    Anyway here's the snippets, TMTOWTDI!
    # heres a functional way my @no_undefs=map{$_ || 0} @has_undefs; # or with a modifier !defined($array[$_]) and $array[$_]=0 foreach 0..$#array; # or more elegantly $array[$_]||=0 foreach 0..$#array; # or with a funky sub # might be slow or big lists though sub undefs2zero {$_[$_]||=0 foreach 0..$#_} undefs2zero(@array);

    You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)

Re: undefined elements in array
by bless$self=>perlmonks; (Novice) on Sep 09, 2001 at 18:09 UTC
    I think you're getting warnings because you're comparing an undefined value. Just try testing for defined:
    $array[$i][$j] = 0 unless defined $array[$i][$j];
Re: undefined elements in array
by tachyon (Chancellor) on Sep 10, 2001 at 02:39 UTC

    This is simple:

    print $array[$i][$j] if defined $array[$i][$j];

    Or alternatively:

    if (defined $array[$i][$j]) { print $array[$i][$j]; } else { print ''; }




      mapping function can quite nice for this type of thing..
      e.g. @a = (1,2,3,undef,5); print $#a; @b = map(defined, @a); print join(',',@b);

        I think you meant grep, not map:

        0:14 >perl -wE "@a = (1,2,3,undef,5); say scalar @a; @b = map(defined +, @a); print join(',', @b);" 5 1,1,1,,1 0:14 >perl -wE "@a = (1,2,3,undef,5); say scalar @a; @b = grep(define +d, @a); print join(',', @b);" 5 1,2,3,5 0:14 >

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://111269]
Approved by root
[LanX]: smoking in movies

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2017-12-13 20:03 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (378 votes). Check out past polls.