Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, was wondering how i could manipulate the following text:
xxx ; xxx { xxx } xxx ( xxx ; xxx
so that the output is something like:
xxx ; xxx { xxx } xxx ( xxx ; xxx
or
xxx ; xxx { xxx } xxx ( xxx ; xxx
or
xxx ; xxx { xxx } xxx ( xxx ; xxx
it's randomized.. (i did it in vbs but cant in perl) :( thanks for any help

Replies are listed 'Best First'.
Re: inserting new lines
by JavaFan (Canon) on Apr 20, 2012 at 19:23 UTC
    You want to randomly change spaces into newlines? Something like:
    s/ /rand() < .5 ? ' ' : "\n"/eg;
    should do.
      nah trying to insert newlines after those 3 special characers, but decide what character to insert a newline after randomly, will it be after ';' or } or { ? would it be ; and { or just ; ?
        To randomly select one of the three characters, you can use code like my $char = qw(; { })[rand 3];. You can then interpolate the result into a substitution that uses a look behind.

        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: inserting new lines
by kennethk (Abbot) on Apr 20, 2012 at 19:22 UTC

    What did you try? Is there a rule for where to insert new lines? It's a fairly trivial operation using regular expressions. For example, inserting new lines before semicolons could be done with

    #!/usr/bin/perl use warnings; use strict; my $text = <<EOT; xxx ; xxx { xxx } xxx ( xxx ; xxx' EOT $text =~ s/(?=;)/\n/g; print $text;

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.