Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > insert random blank every few chars

Reply
Thread Tools

insert random blank every few chars

 
 
Dan Jacobson
Guest
Posts: n/a
 
      01-22-2004
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 ]
 
Reply With Quote
 
 
 
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      01-22-2004
[posted & mailed]

On Fri, 23 Jan 2004, Dan Jacobson wrote:

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

--
Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

 
Reply With Quote
 
 
 
 
John J. Trammell
Guest
Posts: n/a
 
      01-22-2004
On Fri, 23 Jan 2004 02:38:26 +0800, Dan Jacobson <(E-Mail Removed)> wrote:
> 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

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      01-22-2004
On Fri, 23 Jan 2004, Dan Jacobson wrote:
>
> 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
 
Reply With Quote
 
Glenn Jackman
Guest
Posts: n/a
 
      01-22-2004
Dan Jacobson <(E-Mail Removed)> wrote:
> 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";
'
 
Reply With Quote
 
dan baker
Guest
Posts: n/a
 
      01-22-2004
Dan Jacobson <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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
 
Reply With Quote
 
Dan Jacobson
Guest
Posts: n/a
 
      01-23-2004
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 " "
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM
receiving ??? chars instead of "special" chars M.Posseth ASP .Net Web Services 3 11-16-2004 07:00 PM



Advertisments