Solved

How to get a random line from a file

Posted on 2002-04-18
3
193 Views
Last Modified: 2010-03-05
How to get a random line from a file?

If found this at www.perl.com

-----------------------------------------
How do I select a random line from a file?  
Here's an algorithm from the Camel Book:

    srand;
    rand($.) < 1 && ($line = $_) while <>;  
----------------------------------------

My file name will be test.txt

How to use this code. How to tell the script code what the file name is and what will be the result line?
 

0
Comment
Question by:mmcw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 6

Expert Comment

by:andreif
ID: 6950433
In your code data is read from standatd input by "<>"
You can open your text file like usualy and change <> to <FileHandler>

So, code looks like:

open(F, "test.txt");
srand;
rand($.) < 1 && ($line = $_) while <F>;
close(F);


Result is in variable $line;

print $line;
0
 
LVL 6

Expert Comment

by:andreif
ID: 6968669
Please, let me know if this doesn't work or something is not clear
--------------------

In your code data is read from standatd input by "<>"
You can open your text file like usualy and change <> to <FileHandler>

So, code looks like:

open(F, "test.txt");
srand;
rand($.) < 1 && ($line = $_) while <F>;
close(F);


Result is in variable $line;

print $line;
0
 
LVL 6

Accepted Solution

by:
andreif earned 100 total points
ID: 6968670
Please, let me know if this doesn't work or something is not clear
--------------------

In your code data is read from standatd input by "<>"
You can open your text file like usualy and change <> to <FileHandler>

So, code looks like:

open(F, "test.txt");
srand;
rand($.) < 1 && ($line = $_) while <F>;
close(F);


Result is in variable $line;

print $line;
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question