Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

randomize length field for substr 1

Status
Not open for further replies.

mikawin

Technical User
Apr 15, 2009
28
US
Hello,

Is it possible to randomize the length field in the

substr($string_variable, start number, length);

I have multiple strings in a file that have 50-70 characters. I would like to randomly extract anything between 25 and 35 characters from the beginning for each of the strings.

Thanks,
Mika
 
Use the 'rand' function to pick a number between 0 and 10, then add that to 25 to get the random number in the range you want.
 
one way:

Code:
my @nums = (25..35);
my $foo = substr $string,0,$nums[int rand(@nums)];


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ishnids suggestion is a bit better, here it is:

Code:
my $foo = substr $string,0,int rand(11)+25;

Note I used 11 instead of 10.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top