Page 1 of 1
Need some help - pick random lines from text files & email
Posted: Wed Mar 08, 2017 9:37 am
by StudioWorks
Hello everybody,
I am kind of new to this so I am hoping that you can give me a hand. The task is this:
I have 5 .txt files, each file contains 10 lines of text. I have to randomly select one line from each file and the these generated lines have to be sent through email to two email addresses.
Can you please provide some pointers ?
Thank you!
Re: Need some help - pick random lines from text files & ema
Posted: Wed Mar 08, 2017 9:50 am
by Celauran
What have you done so far and where are you encountering issues?
Re: Need some help - pick random lines from text files & ema
Posted: Wed Mar 08, 2017 9:59 am
by StudioWorks
So fa I can select random lines from one .txt files however I am having troubles selecting from multiple .txt files, don't know exactly how to do it.
Re: Need some help - pick random lines from text files & ema
Posted: Wed Mar 08, 2017 10:16 am
by Celauran
How are you selecting from the one file? How are you storing the results? Do you have a list of files you could select from?
Re: Need some help - pick random lines from text files & ema
Posted: Wed Mar 08, 2017 10:29 am
by StudioWorks
So far only have this to select a random line from just one file:
<?php
$content = file("file.txt");
$line = $content[rand(0, count($content) - 1)];
?>
As i said i don't know how to handle this with multiple files or how to send the result through email..
Re: Need some help - pick random lines from text files & ema
Posted: Wed Mar 08, 2017 10:39 am
by Celauran
What if you used an array to store the list of filenames and another array to store the randomly selected lines?
Re: Need some help - pick random lines from text files & ema
Posted: Wed Mar 08, 2017 10:41 am
by StudioWorks
Hey,
And how would I do that ? As i said, i'm new to this. I'm not expecting something for free, I can pay for your help if you want.
Re: Need some help - pick random lines from text files & ema
Posted: Wed Mar 08, 2017 11:09 am
by Celauran
I'm not looking for payment. I'm trying to help you arrive at the answer yourself rather than doing your homework for you.
Re: Need some help - pick random lines from text files & ema
Posted: Wed Mar 08, 2017 12:28 pm
by StudioWorks
Hey, thank you for your help! i would gladly stay and study this, it's not homework it's a favor for a friend and this is not exactly my area and it needs to be done by tomorrow, that's why the desperation

Re: Need some help - pick random lines from text files & ema
Posted: Wed Mar 08, 2017 12:47 pm
by Celauran
Instead of hardcoding "file.txt" into your file() call, create an array of all the files and iterate over it. Create a separate array for the lines you retrieve and push new items into the array with each iteration.
Something like this should give you an idea
Code: Select all
$files = ['file.txt', 'file2.txt', 'file3.txt'];
$lines = [];
foreach ($files as $file) {
$contents = file($file);
array_shuffle($contents);
$lines[] = array_pop($content);
}
Re: Need some help - pick random lines from text files & ema
Posted: Wed Mar 08, 2017 1:37 pm
by StudioWorks
I will give this a try and let you know how it goes!! Thank you so much for your help and patience!! ( I wasn't joking when I said that i don't expect things for free! )
Re: Need some help - pick random lines from text files & ema
Posted: Thu Mar 09, 2017 9:03 am
by StudioWorks
Thank you, you've been very helpful. I started out from your advice and I managed to get the job done.
Thank you again!
Re: Need some help - pick random lines from text files & ema
Posted: Thu Mar 09, 2017 9:03 am
by Celauran
Glad to hear it!