Gossamer Forum
Home : General : Perl Programming :

A quick question

Quote Reply
A quick question
I have a problem with a little script that I wrote the script is as follows
# My txt file
$file = "First.txt";
open (FILE, $file);
@LINES=<FILE>; #Read in the entire data file
close(FILE);
srand; #do the random pick up
print "$LINES[int rand(@LINES)]";

this then prints out the a random line from the txt file is there any way that is can read out the whole of the txt file
and print it all to screen rather than just to print out one line out at a time

Quote Reply
Re: A quick question In reply to
Hi,

I assume you want to return the entire contents of the file, not random lines as in the example above. You can do this by using the "undef" (undefined) statement prior to opening the file file. This will tell Perl to ignore the newline or hard returns that typically denote the end of a line. Try the example below with your text file.


$file = "First.txt";
undef $/;
open (FILE, $file);
@LINES=<FILE>; #Read in the entire data file
close(FILE);
$/="\n";

#Now print whats in "@LINES" to the screen. We still need to tell perl to print all the lines in the file.
print "Content-type: text/html\n\n";
foreach $line(@LINES){
print "$line";
}
exit;

Hope this helps,

John
Quote Reply
Re: A quick question In reply to
No I still need to print the output in a ramdom order the script workds fine but i need to out put it in a radom order the script above will just print that whole file i need it to be printed out in a random order thank you again for your help.

Quote Reply
Re: A quick question In reply to
print sort { (rand > 0.5) ? -1 : 1 } @LINES;

will print out your array in random order.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: A quick question In reply to
I have the same need I have tried the code as follows
$file = "numbers.txt";
undef $/;
open (FILE, $file);
@LINES=<FILE>; #Read in the entire data file
close(FILE);
print sort { (rand > 0.5) ? -1 : 1 } @LINES;

my file looks like this

1
2
3
4
..

and when I run the above code it will print
1
2
3
4
5
6
..



Quote Reply
Re: A quick question In reply to
It works for me:

perl -e '@LINES = 1 .. 5; print sort { (rand > 0.5) ? -1 : 1 } @LINES'

I get the numbers in random output. Must be something else you aren't showing us..

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: A quick question In reply to
works for me now as well after # out undef $/;


$file = "numbers.txt";
#undef $/;
open (FILE, $file);
@LINES=<FILE>;
close(FILE);
print sort { (rand > 0.5) ? -1 : 1 } @LINE


can you tell me what the line and the sintax you are using
eg what this is up 2
rand > 0.5) ? -1 : 1 }


Quote Reply
Re: A quick question In reply to
Sure, sort takes a code block that you can use to sort it. It uses $a and $b and you return 1 if $a is greater, -1 if $b is greater, and 0 if they are equal.

We don't care about the values, so we just return randomly -1 or 1.

The code:

(rand > 0.5) ? 1 : -1;

does the trick. rand() is a perl function that returns a random number between 0 and 1. The ? is a perl operator that says if the left hand side is true, then evaluate the first part, if it's false, evaluate the second part. So if the random number is > 0.5, we return 1, otherwise we return a -1.

Hope that helps,

Alex

--
Gossamer Threads Inc.