ctowncoug has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks. I am new to the monastery and would appreciate some help with my code. When compiling my code I am getting the following error: "Use of uninitialized value in join or string at ./palin.pl line 39. I have toyed around with the line in question and nothing has worked for me so far. I am extremely new to Perl and would appreciate some help. The code compiles and runs with warnings turned off, but I am trying to follow best practices and leave it on. Please find my code with this post:
#!/usr/bin/perl use strict; my @palindromes = (); my @names = qw( Aaron Adam Amir Ann Anna Art Axel Brent Brian Ch +arles Chuck Cris Dave Don Dori Doug Eva Fred Gregor Ia +n Ima Ira Jarl Jason Jay Jeff Joe Joel John Kari Kim K +irk Kris Lar Leo Lex Lin Mara Marc Mark Mary Michael + Mike Miki Mott Naim Nils Nora Norm Omar Pete Raj Ralf + Ram Ramon Rik Rod Roger Ron Tom Tony Tran Travis Sar +ah Palin ); for my $word1 (@names) { for my $word2 (@names) { for my $word3 (@names) { #if ($word1 eq $word2) #next; if ( palin( $word1, $word2, $word3 ) ) { push @palindromes, $word1,'',$word2,'', $word3; print "$word1 $word2 $word3\n"; last; } else { if ( palin( $word1, $word2 ) ) { push @palindromes, $word1,'',$word2; print "$word1 $word2\n"; last; } } } } } sub palin { my $names1 = shift; my $names2 = shift; my $names3 = shift; my $string = join( '',$names1,$names2,$names3); my $reversestring = reverse($string); return ( uc($reversestring) eq uc($string)); }
I am currently trying to format the post correctly, I apologize for the current format.

Replies are listed 'Best First'.
Re: Use of uninitialized value in join or string
by dreadpiratepeter (Priest) on Nov 05, 2008 at 16:35 UTC
    You need to put code tags around your code so that we can read your posts, but...
    the problem is that you are not always passing 3 params into the palin sub, but you are always expecting 3. Either always pass 3, or put a $names3 ||= '' line in the sub.


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: Use of uninitialized value in join or string
by ww (Archbishop) on Nov 06, 2008 at 14:39 UTC

    This may be a wasted node when OP or the Janitors provide code tags, but (from the xml view) this appears to be what OP was trying:

    #!/usr/bin/perl use strict; my @palindromes = (); my @names = qw( Aaron Adam Amir Ann Anna Art Axel Brent Brian Ch +arles Chuck Cris Dave Don Dori Doug Eva Fred Gregor Ia +n Ima Ira Jarl Jason Jay Jeff Joe Joel John Kari Kim K +irk Kris Lar Leo Lex Lin Mara Marc Mark Mary Michael + Mike Miki Mott Naim Nils Nora Norm Omar Pete Raj Ralf + Ram Ramon Rik Rod Roger Ron Tom Tony Tran Travis Sar +ah Palin ); for my $word1 (@names) { for my $word2 (@names) { for my $word3 (@names) { #if ($word1 eq $word2) #next; if ( palin( $word1, $word2, $word3 ) ) { push @palindromes, $word1,'',$word2,'', $word3; print "$word1 $word2 $word3\n"; last; } else { if ( palin( $word1, $word2 ) ) { push @palindromes, $word1,'',$word2; print "$word1 $word2\n"; last; } } } } } sub palin { my $names1 = shift; my $names2 = shift; my $names3 = shift; my $string = join( '',$names1,$names2,$names3); my $reversestring = reverse($string); return ( uc($reversestring) eq uc($string)); }

    My take is that OP seeks to generate all possible permutations of the elements of @names two OR three at a time; to test for palindromes in the sub; and (at lines 19 and 25), print those which pass the test.

    That may even be correct - :-) - but OP fails to tell us and it seems to me obvious (YMMV) that this code does not accomplish that for reasons well beyond the scope of the warning cited as an "error."

    For example, running withOUT -w or use warnings, this produces such non-palindromic strings as:

    Ann Ann Anna
    Ann Anna

    as well as valid output (once spaces are stripped):

    ...Kirk Rik
    Leo Joel
    Lex Ann Axel
    Lex Axel
    Mara Ram...

    but skips such apparently acceptable permutations as

    Anna Anna Anna

    despite the fact that lines 14 and 15, skipping over dups in $words1 and $words2, are commented out.)

    All the above leads me to two questions:
    Does this interpretation make sense?
    and...
    Will OP, please, explain the original intent?

    It is much easier to offer help -- even if beyond the scope of the question about the "error" -- when we know what you are trying to do. To paraphrase a former US Veep, A node is a terrible thing to waste.

Re: Use of uninitialized value in join or string
by Anonymous Monk on Nov 06, 2008 at 01:30 UTC
    HTML code tags will look like <code> some_code(); </code> or, more briefly, <c> some_code(); </c> and they make your posting look a heck of a lot better and more readable — and more likely to garner a response. It should be pretty simple to put them in.

    See also Writeup Formatting Tips and associated links if you haven't already.

    Once you've taken a look at that stuff, please go back and edit your node: How do I change/delete my post?.