Solved

How to get a random line from a file

Posted on 2002-04-18
3
181 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
  • 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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