insert random blank every few chars

J

Jeff 'japhy' Pinyan

[posted & mailed]

How can I introduce a random blank every few chars?
I want to chop a string into words oh, 3 to 9 chars long.
echo fknfgkljstfklmzkbmsfb|perl -e ...
fkn fgkljst fklmzk bmsfb

The primary problem with what you're asking is that you need to make sure
all the resulting chunks are the proper length. For instance,

abcdefghijklmno => abcdef ghijk lmn o

has a chunk of 6, then 5, then 3, but then we're left with ONE character.
 
J

John J. Trammell

How can I introduce a random blank every few chars?
I want to chop a string into words oh, 3 to 9 chars long.
echo fknfgkljstfklmzkbmsfb|perl -e ...
fkn fgkljst fklmzk bmsfb
[workaround for morse code practice
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=222128 ]

My humble solution:

#!/usr/bin/perl
use strict;
use warnings;

print addspaces($_) while <DATA>;

sub addspaces {
my ($loc,$str,@int) = (0,$_[0],3,4,5,6,7,8,9);
{
$loc += $int[ rand(@int) ];
last unless $loc < length($str);
substr($str,$loc++,0) = " ";
redo;
}
return $str;
}

__DATA__
alkalalsdfadsfahsdfjasdfhlasjkf
vxmznvcbxcmvbnxcmvbxcvb
 
P

Paul Lalli

How can I introduce a random blank every few chars?
I want to chop a string into words oh, 3 to 9 chars long.
echo fknfgkljstfklmzkbmsfb|perl -e ...
fkn fgkljst fklmzk bmsfb
[workaround for morse code practice
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=222128 ]


#!/usr/bin/perl
use warnings;
use strict;

my $string = $ARGV[0];

my $pos = 0;
while ($pos < length($string)){
$pos += int(rand 6) + 3;
last if $pos >= length($string);
$string = substr($string, 0, $pos) . " " . substr($string, $pos);
}

print "$string\n";


That could probably be shortened up a bit, but it's a good start.

Paul Lalli
 
G

Glenn Jackman

Dan Jacobson said:
How can I introduce a random blank every few chars?
I want to chop a string into words oh, 3 to 9 chars long.
echo fknfgkljstfklmzkbmsfb|perl -e ...
fkn fgkljst fklmzk bmsfb

echo qwersdafdrqwerasdvcxvasdfqeasdfaxcv | perl -ne '
while ($_) {
my $len = 3 + int rand 7;
print substr($_, 0, $len), " ";
substr($_, 0, $len) = "";
}
print "\n";
'
 
D

dan baker

Dan Jacobson said:
How can I introduce a random blank every few chars?
---------------

gee, are you sure you arent trying to develop another tactic to
obsfucate words for sending SPAM ?

d
 
D

Dan Jacobson

Jeff> has a chunk of 6, then 5, then 3, but then we're left with ONE character.

No big deal at the end of a lengthy Morse code practice session.

For stream of input with no newlines, (cwgen|tr -d ' '), my stab at it is:

$low = 3;
$high = 9;
while (1) {
{
for ( 1 .. $low + int( rand( $high - $low + 1 ) ) ) {
print getc || exit;
}
print " "
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top