User Control Panel
Advertisements

HELP US, HELP YOU!

Random line from a file...?

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl
View unanswered posts
Author Message
Cheatsfp
Member
Member


Joined: 30 Dec 2003
Posts: 109

Reputation: 30.8Reputation: 30.8Reputation: 30.8

PostPosted: Wed Dec 31, 2003 6:25 pm    Post subject: Reply with quote

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?

Smile
Back to top
eric256
The Keymaker
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Wed Dec 31, 2003 6:27 pm    Post subject: Reply with quote

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
Member


Joined: 30 Dec 2003
Posts: 109

Reputation: 30.8Reputation: 30.8Reputation: 30.8

PostPosted: Wed Dec 31, 2003 6:34 pm    Post subject: Reply with quote

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! Smile
Back to top
Keenie
Almost An Agent
Almost An Agent


Joined: 31 Oct 2003
Posts: 1071

Reputation: 52.4

PostPosted: Wed Dec 31, 2003 6:59 pm    Post subject: Reply with quote

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
God Like


Joined: 12 Nov 2003
Posts: 553

Reputation: 41.3Reputation: 41.3Reputation: 41.3Reputation: 41.3

PostPosted: Wed Dec 31, 2003 7:21 pm    Post subject: Reply with quote

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. Razz
Back to top
Cheatsfp
Member
Member


Joined: 30 Dec 2003
Posts: 109

Reputation: 30.8Reputation: 30.8Reputation: 30.8

PostPosted: Wed Dec 31, 2003 7:24 pm    Post subject: Reply with quote

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?

Smile
Back to top
Nate
God Like
God Like


Joined: 12 Nov 2003
Posts: 553

Reputation: 41.3Reputation: 41.3Reputation: 41.3Reputation: 41.3

PostPosted: Wed Dec 31, 2003 7:28 pm    Post subject: Reply with quote

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
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Wed Dec 31, 2003 7:28 pm    Post subject: Reply with quote

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
Member


Joined: 30 Dec 2003
Posts: 109

Reputation: 30.8Reputation: 30.8Reputation: 30.8

PostPosted: Wed Dec 31, 2003 7:33 pm    Post subject: Reply with quote

Ah, okay I get it now. Thank you!

Smile
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group