Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read a file and make.....

Status
Not open for further replies.

Guest_imported

New member
Joined
Jan 1, 1970
Messages
0
Hi all.
I'm stuck in my perl script.
This is my problem:

I want to open up a plain-text file, the file looks like this

1.word1
2.word2
3.word3
4.word4
5.word5
6.word6
and so on...until row 50

I want to open this file and take 4 rows in a random way AND I always want line 5.

So my output shold look like

2.word2
4.word4
5.word5 (this one must always be included)
9.word9

Now I want to make randomise the output so it will look like this

4.word4
5.word5
9.word9
2.word2

I know,this is not easy but I can't solve this one without any help

A BIG thanks to everyone
/PP







Does anyone knows how to do this ?
 
something like this might work:[tt]
open FILE, "file.txt" or die "no file open: $!";
my @file = <FILE>;
close FILE;
my @results;
push @results, splice(@file, 4, 1);
push @results, splice(@file, int(rand @file), 1);
push @results, splice(@file, int(rand @file), 1);
push @results, splice(@file, int(rand @file), 1);
while (@results)
{
print splice(@results, int(rand @results), 1);
}[/tt]
&quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Thanks Stillflame, works great :)
There is one more thing about this random thing I would like to do.
I know get outputs like:
3.5.14.25.

I'm having trouble to set each output to a value ,like this
3. = $value1
5. = $value5
14. = $value14
25. = $value25

Thanks again
/PP-PP


 
Opps,wrong
Should be like
$value1 = 3.
$value2 = 4.
$value3 = 14.
$value4 = 25.

/PP-PP


 
er, okay. my code is getting a little convoluted, but for the last 'while' loop, change it to something like this:[tt]
my @tmp_array;
while (@results)
{
push @tmp_array, splice(@results, int(rand @results), 1);
}
my ($var1, $var2, $var3, etc.) = @tmp_array;[/tt]


;-) &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
hey
I am biggner and want to know simple way to read form one file and write to another.

how am i going to do that?
 
Sunnu:

Try this:

[tt]
open(FILE,&quot;file.txt&quot;) || die(&quot;failed to open file: $!&quot;);
@data = <FILE>;
close(FILE);

open(FILE,&quot;>>newfile.txt&quot;) || die(&quot;failed to open file: $!&quot;);
print FILE @data;
close(FILE);

[tt]

The first set of instructions opens the file via the open() function and stores the file handle in FILE, then grabs all of the data from the file and puts it in an array called @data.

The second set of instructions opens a new file, if it doesn't exist, it creates it because of the >> and then prints the data to the file.

Hope this helps.

-Vic vic cherubini
krs-one@cnunited.com
====
Knows: Perl, HTML, JavScript, C/C++, PHP, Flash
====
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top