Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

unititialized value warning

by wrinkles (Pilgrim)
on Mar 14, 2010 at 01:23 UTC ( #828500=perlquestion: print w/ replies, xml ) Need Help??
wrinkles has asked for the wisdom of the Perl Monks concerning the following question:

Hi fellow monks,

What is my code doing that warrants a warning, and how do I fix it? Thanks. Abridged warnings:

Use of uninitialized value in length at ./jumble.pl line 34, <DATA> li +ne 1. Use of uninitialized value in split at ./jumble.pl line 34, <DATA> lin +e 1. Use of uninitialized value in concatenation (.) or string at ./jumble. +pl line 36, <DATA> line 1. Use of uninitialized value in split at ./jumble.pl line 33, <DATA> lin +e 1.

The offending code

use strict; use warnings; use List::Util 'shuffle'; while (<DATA>) { my @newline = split(/\b/); for my $word (@newline) { my $first = ""; my $rem = ""; my $middle = ""; my $last = ""; ($first, $rem) = split /(?<=\b\w)/, $word; ($middle, $last) = split /(?=(\w'\w+|\w)$)/, $rem; ((length $middle) == 2) ? $middle = reverse $middle : ($middle = join "", shuffle(split //, $middle)); print "$first$middle$last"; } }
__END__ I couldn't believe that I was actually understanding what I was readin +g. The phenomenal power of the human mind, according to research at Cambr +idge University, suggests that it doesn't matter in what order the letters in a word ar +e, as long as the first and last letters are in the right place. The rest can be a t +otal mess, and you can still read it without a problem. This is because the human + mind does not read every letter by itself, but the word as a whole. Amazing, huh +? Yeah, and I always thought spelling was important! Check apostrope and punctuation: Jame's, They're, we'll they'd.

And here's the output, with warnings off: I cdolun't beievle taht I was aalulcty uedsnadnnitrg waht I was rdaenig. The peeaonhmnl peowr of the hmuan mnid, adcircnog to rerecash at Cibmdarge Uteirsvniy, ssugetgs taht it dosen't mettar in waht odrer the lterets in a wrod are, as lnog as the fisrt and lsat ltteres are in the rghit pcale. The rset can be a toatl mses, and you can siltl raed it woihtut a pboerlm. Tihs is bceusae the hmuan mnid deos not raed eervy lteetr by iseltf, but the wrod as a wlhoe. Ainzamg, huh? Yaeh, and I ayawls tguhhot splileng was intpaormt! Cchek asoptorpe and puttiunoacn: Jmae's, Tehy're, we'll tehy'd.

Comment on unititialized value warning
Select or Download Code
Re: unititialized value warning
by ikegami (Pope) on Mar 14, 2010 at 01:45 UTC

    "Note that splitting an EXPR that evaluates to the empty string always returns the empty list, regardless of the LIMIT specified."

    $rem holds an empty string, so an empty list is assigned to ($middle, $last), setting $middle and $last to undef.

    Fix:

    use strict; use warnings; use List::Util qw( shuffle ); while (<DATA>) { s/(?<=\w)(\w+)(?=\w)/ join('', shuffle($1=~m{.}sg)) /eg; print; } __DATA__ I couldn't believe that I was actually understanding what I was reading. The phenomenal power of the human mind, according to research at Cambridge University, suggests that it doesn't matter in what order the letters in a word are, as long as the first and last letters are in the right place. The rest can be a total mess, and you can still read it without a problem. This is because the human mind does not read every letter by itself, but the word as a whole. Amazing, huh? Yeah, and I always thought spelling was important! Check apostrophe and punctuation: Jame's, They're, we'll they'd.

    By the way, try replacing

    shuffle($1=~m{.}sg)
    with
    sort $1=~m{.}sg
    It'll probably be harder to read.
      Thanks ikegami, I just needed to check the variables. This fixed it:
      use strict; use warnings; use List::Util 'shuffle'; while (<DATA>) { my @newline = split(/\b/); for my $word (@newline) { my $first = ""; my $rem = ""; my $middle = ""; my $last = ""; ($first, $rem) = split /(?<=\b\w)/, $word if $word; ($middle, $last) = split /(?=(\w'\w+|\w)$)/, $rem if $rem; ((length $middle) == 2) ? $middle = reverse $middle : ($middle = join "", shuffle(split //, $middle)) if $middle; print "$first" if $first; print "$middle" if $middle; print "$last" if $last; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://828500]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-09-23 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls