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

Hi guys....

1. I am trying to form a sequence with letter DA of width 100..

2. I want to restrict the occurrence of 'DA' in this 100(characters) to 20%, 30% and so on...

i.e

DADADDDADDD
There are 3 DA's in the string...

I want to have control on occurrence of DA and generate sequence randomly for 100 characters

#!/usr/local/bin/perl my @chars = ('A','D',); my $string = join '', map $chars[ rand @chars ], 1 .. 100; while ($string =~ /DA/g) { $count++ } print "There are $count DA's in the string \n "; print $string ;
But i am not able to have control on the occurrence of 'DA'

It keeps changing randomly with this coding

Replies are listed 'Best First'.
Re: Finding a pattern in a string
by FunkyMonk (Bishop) on Oct 27, 2007 at 08:10 UTC
    It's not clear what exactly you're after, but my take is that you want to know the offsets within $string where $den occurs. The following code does just that (I've made $string shorter for posting purposes).
    my @chars = ('A','D'); my $string = join '', map $chars[ rand @chars ], 1 .. 20; my $den = 'DA'; my @positions; while ( $string =~ /$den/g ) { push @positions, pos( $string ) - length $den; } if ( @positions ) { print "'$den' was found at offsets @positions\n"; } else { print "DA is not present\n\n\n"; } print +(0 .. 9) x 5, "\n"; print "$string\n";

    Sample run:

    'DA' was found at offsets 2 5 7 10 01234567890123456789012345678901234567890123456789 ADDAADADADDAAAAAAAAA

    Notes

    • pos returns the offset of the end of the match
    • while is used to loop over the string
    • I used a /g modifier to the m// operator to search the string. See perlre and perlreftut
Re: Finding a pattern in a string
by deorth (Scribe) on Oct 27, 2007 at 07:58 UTC
    Hi Kenin,

    Two things :
    1) The right place for this sort of question would be 'seekers of perl wisdom'. PerlMonks discussion is for discussion *about* perlmonks itself.

    2) Try to format your message a bit better. Because perlmonks is 'sort of' html (forgive me purists if I'm inaccurate there ;) it has compressed your whole message onto one line. This makes the code pretty hard to read.

    For both of the above, you should probably review Understanding and Using PerlMonks & Writeup Formatting Tips

    I'm reposting the now so that its easier to discuss :

    #!/usr/local/bin/perl open CHITIN , ">CHITIN"; my @chars = ('A','D',); my $string = join '', map $chars rand @chars , 1 .. 100; $den =('DA'); + $count=$DA; if ($string =~/$den/) { print "DA was found \n\n"; } else { print "DA is not present\n\n\n"; } print $count; print $string;
Re: Finding a pattern in a string
by GrandFather (Saint) on Oct 27, 2007 at 08:03 UTC

    What do you intend:

    my $string = join '', map $chars rand @chars , 1 .. 100;

    to do?


    Perl is environmentally friendly - it saves trees

      I personally believe that due to the OP's failing to put his code in <code> tags, it rendered like that, but it's fairly obvious that he wrote and meant

      my $string = join '', map $chars[rand @chars], 1 .. 100;

      In doubt, check the xml source of the post.

Re: Finding a pattern in a string
by deorth (Scribe) on Oct 27, 2007 at 08:14 UTC
    Dang.. I write the reformatted script, then pop off for five mins to work out a solution. And two of you get there before me :)

    I'll never get first post round here :)

      That often happens here,
      There are some very fast brains and even faster coders but you should still post your reply.
Re: Finding a pattern in a string
by Anonymous Monk on Oct 27, 2007 at 07:47 UTC
    what is the problem?