Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: unititialized value warning

by ikegami (Patriarch)
on Mar 14, 2010 at 01:45 UTC ( [id://828505]=note: print w/replies, xml ) Need Help??


in reply to unititialized value warning

"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.

Replies are listed 'Best First'.
Re^2: unititialized value warning
by wrinkles (Pilgrim) on Mar 14, 2010 at 02:33 UTC
    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://828505]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-23 21:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found