insert random blank every few chars

Discussion in 'Perl Misc' started by Dan Jacobson, Jan 22, 2004.

  1. Dan Jacobson

    Dan Jacobson Guest

    Dan Jacobson, Jan 22, 2004
    #1
    1. Advertising

  2. [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)
     
    Jeff 'japhy' Pinyan, Jan 22, 2004
    #2
    1. Advertising

  3. On Fri, 23 Jan 2004 02:38:26 +0800, 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 ]
    >


    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
     
    John J. Trammell, Jan 22, 2004
    #3
  4. Dan Jacobson

    Paul Lalli Guest

    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
     
    Paul Lalli, Jan 22, 2004
    #4
  5. 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


    echo qwersdafdrqwerasdvcxvasdfqeasdfaxcv | perl -ne '
    while ($_) {
    my $len = 3 + int rand 7;
    print substr($_, 0, $len), " ";
    substr($_, 0, $len) = "";
    }
    print "\n";
    '
     
    Glenn Jackman, Jan 22, 2004
    #5
  6. Dan Jacobson

    dan baker Guest

    Dan Jacobson <> wrote in message news:<>...
    > 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
     
    dan baker, Jan 22, 2004
    #6
  7. Dan Jacobson

    Dan Jacobson Guest

    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 " "
    }
    }
     
    Dan Jacobson, Jan 23, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,326
    Tim Rentsch
    Sep 23, 2005
  2. Murali
    Replies:
    2
    Views:
    605
    Jerry Coffin
    Mar 9, 2006
  3. globalrev
    Replies:
    4
    Views:
    795
    Gabriel Genellina
    Apr 20, 2008
  4. Paulo da Silva
    Replies:
    6
    Views:
    388
    Paulo da Silva
    Jun 6, 2010
  5. VK
    Replies:
    15
    Views:
    1,277
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page