in reply to Re: Re: Function over all letters in a string
in thread Function over all letters in a string

What do you define a letter to be?

I found that part of the spec ambiguous and so made up a convenient definition. *shrug*

  • Comment on Re: Re: Re: Function over all letters in a string

Replies are listed 'Best First'.
Re: Re: Re: Re: Function over all letters in a string
by bart (Canon) on Dec 01, 2003 at 10:50 UTC
    I found that part of the spec ambiguous and so made up a convenient definition.
    You also apparently made a convenient definition of the phrase "random number". :)

    Personally, I'd like something that has a chance of producing a long string, but favouring shorter ones. Hence, a simple 1 + int rand 12 won't do. Rather, I'm tempted to use an exponential function:

    $_ = 'cat'; s/(.)/$1 x int(12**rand)/ge; print;
    Which, for example, prints
    caaatttttttt
    
      But that still puts an arbitrary (low) limit on the number of repetitions. The code below doesn't:
      #!/usr/bin/perl use strict; use warnings; $_ = 'cat'; s/(.)/my $t = $1; $t .= $1 while 0.95 > rand; $t/ge; print "$_\n"; __END__ cccccccccccccaaaaat

      Abigail

      You would have a larger range of possible numbers with:
      $string =~ s=(\w)= $1 x (1 - 1/log(rand)) =eg;