LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 04-17-2006, 06:38 PM   #1
GUIPenguin
Member
 
Registered: Aug 2004
Location: Maine
Distribution: Gentoo Linux
Posts: 239

Rep: Reputation: 30
Help with perl script


Hello. This is my secound day learning perl. So I thought I would write a simple script to use an array and randomly choose one of the 26 availible letters which will be checked against what you wanted to find, then print how many times it took.

here is what I have to find one letter of your choosing.

Code:
#!/usr/bin/perl

@array=('A' .. 'Z' );

$times = 0;

while ($hold ne "A") {



        $randnum = int( rand(26) );

        $hold = @array[$randnum];

        $times++;

}

print("We found A! Tt took $times times \n");
Cool. This works fine for what I want. Now here is my question: How can I get this same model to work for a string of more then 1 letters.
For example, locating a string that is 2 letters, such as AB or ABC. Like maybe I would like to see how long it takes to randomly find my name 'JOHN'. Hope I provided enough info about how goal.

Last edited by GUIPenguin; 04-17-2006 at 07:56 PM.
 
Old 04-17-2006, 08:00 PM   #2
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Assuming you still want to find A, but want to generate random strings of random length from 1 to MAX_LEN characters, maybe something like this:
Code:
#!/usr/local/bin/perl
use strict;

my @array = qw/A B C D E F G H I J K L M N O P Q R S T U V W X Y Z/;
my $MAX_LEN = 5; # Maximum letters allowed in the string
my $times = 0;
my $x = 0; # counter
my $hold;
my $string_length;
my $randnum;

while ($hold ne "A") {
   $string_length = int( rand( $MAX_LEN - 1)) + 1; # if we want 1 - 5, get 0 - 4 and add one
   print "string_length = $string_length\n";
   $hold = "";
   for( $x = 0; $x < $string_length; $x++ ) {
      $randnum = int( rand( 25 )); # rand( x ) returns 0 to x; we only want 0 to 25 here, really

      $hold = $hold . @array[$randnum];
   }
   print "hold = $hold\n";
   $times++;
}

print("We found A! It took $times times \n");
I'm not 100% clear on what you are looking for, so if that's not it, please do clarify a tad. Hope that helps.
 
Old 04-17-2006, 09:07 PM   #3
GUIPenguin
Member
 
Registered: Aug 2004
Location: Maine
Distribution: Gentoo Linux
Posts: 239

Original Poster
Rep: Reputation: 30
sweet. Thanks a ton. Im going to change it around a little bit to suit my needs and to give me a little better idea about how it works.
 
Old 04-18-2006, 02:34 AM   #4
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Actually, int(rand(x)) returns an integer from 0 to x-1

Also (because I am a picky Perl programmer), I will point out you can write that for loop with something like this:
Code:
$hold = join '', map $array[rand @array], (1..$string_length);
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
Help with Perl Script dannyp Programming 0 07-18-2004 07:36 PM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM
perl script... killjoy Programming 0 03-29-2001 03:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration