|
| Author |
Message |
Cheatsfp Member

Joined: 30 Dec 2003 Posts: 109
   
|
Posted: Wed Dec 31, 2003 6:25 pm Post subject: |
|
|
Okay,
I saw the code on the forums for using a random line for a file. I just have a few questions about it...
| Code: | | #open our file <br />my $filename = "test.pl"; <br />open (FILE, "< $filename") or die "Could not open $filename"; <br /><br />#load the lines into a array <br />my @lines = <FILE>; <br /><br />#close the file <br />close (FILE); <br /><br />#remove any extra lines <br />chomp (@lines); <br /><br />#get our random line <br />my $str = $lines[int(rand(scalar(@lines)))]; <br /><br />#print our line<br />print $str; <br /> |
The file where it gets a random line from a/the file is called 'test.pl'?Or is it 'filename'? So that is where I put the random lines of words? Or am I suppose to put it in a different file?
 |
|
| Back to top |
|
 |
eric256 The Keymaker

Joined: 03 May 2006 Posts: 2292 Location: Colorado
     
|
Posted: Wed Dec 31, 2003 6:27 pm Post subject: |
|
|
It will open the file whose name is stored in the variable $filename. So in the above example it looks for a file called test.pl _________________ Eric256
Proud previous owner and current admin of Bot-depot.com |
|
| Back to top |
|
 |
Cheatsfp Member

Joined: 30 Dec 2003 Posts: 109
   
|
Posted: Wed Dec 31, 2003 6:34 pm Post subject: |
|
|
I think I get what you are trying to say. So, if the random words go into the file named 'test.pl' where does this code/source code go?
And, how am I to write the random words/sentences? Am I to just write a sentence and then just skip a line?
Thank you!  |
|
| Back to top |
|
 |
Keenie Almost An Agent

Joined: 31 Oct 2003 Posts: 1071
 
|
Posted: Wed Dec 31, 2003 6:59 pm Post subject: |
|
|
the code goes anywhere you want to find a random line and you can change 'test.pl' to any file you want.
as for setting up the file just make it like this..
| Quote: | line 1 line 2 line 3 line 4 |
then it would choose a random line and reply the text on that line |
|
| Back to top |
|
 |
Nate God Like

Joined: 12 Nov 2003 Posts: 553
    
|
Posted: Wed Dec 31, 2003 7:21 pm Post subject: |
|
|
I think the use of .PL is confusing him. Whoever wrote that first post should have used TXT or something. PL = Perl, not a bunch of plaintext lines.  |
|
| Back to top |
|
 |
Cheatsfp Member

Joined: 30 Dec 2003 Posts: 109
   
|
Posted: Wed Dec 31, 2003 7:24 pm Post subject: |
|
|
Now I am getting really confused... :blink:
Okay,
So, I need to create a file named 'test.pl' and put the code in it. And then create a file named 'something.txt' and put the senteneces/words in it?
 |
|
| Back to top |
|
 |
Nate God Like

Joined: 12 Nov 2003 Posts: 553
    
|
Posted: Wed Dec 31, 2003 7:28 pm Post subject: |
|
|
Yeah. Using the code mentioned at the top of this thread,
Change $filename to equal "file.txt"
In file.txt write something like this:
| Code: | | Random line 1<br />Something here<br />Third line<br />Quatro<br />Cinco<br />Sixth line<br />All these will be randomly chosen |
And that's pretty much how to do it. On each line in the file will be a different string which may be chosen randomly. |
|
| Back to top |
|
 |
eric256 The Keymaker

Joined: 03 May 2006 Posts: 2292 Location: Colorado
     
|
Posted: Wed Dec 31, 2003 7:28 pm Post subject: |
|
|
Perl is just a bunch of plain text lines.
Cheatsfp: Put that code in any file where you want ot use it. Then put the text you want in a file with any name you like. Then change the $filename to = whatever the name of the file is that you put the lines in. _________________ Eric256
Proud previous owner and current admin of Bot-depot.com |
|
| Back to top |
|
 |
Cheatsfp Member

Joined: 30 Dec 2003 Posts: 109
   
|
Posted: Wed Dec 31, 2003 7:33 pm Post subject: |
|
|
Ah, okay I get it now. Thank you!
 |
|
| Back to top |
|
 |
|