how to combine items randomly?
-
Wanda
- Posts: 55
- Joined: 08 Jan 2011 01:08
how to combine items randomly?
hi folks.
i need to combine some files but i'd like to have it made randomly. can one of you please help me with this?
please note that sometimes list 1 has more or less files than list 2 so empty items are expected.
list 1
item a
item b
item c
list 2
photo 1
photo 2
photo 3
result:
item a has photo 2
item b has photo 3
item c has photo 1
i will also need another one to randomly form pairs out of a list of names:
Fernanda
Rosa
Ana
Jaque
Raila
Clara
result:
Fernanda and Clara
Rosa and Raila
Ana and Jaque
please.
i need to combine some files but i'd like to have it made randomly. can one of you please help me with this?
please note that sometimes list 1 has more or less files than list 2 so empty items are expected.
list 1
item a
item b
item c
list 2
photo 1
photo 2
photo 3
result:
item a has photo 2
item b has photo 3
item c has photo 1
i will also need another one to randomly form pairs out of a list of names:
Fernanda
Rosa
Ana
Jaque
Raila
Clara
result:
Fernanda and Clara
Rosa and Raila
Ana and Jaque
please.
Last edited by Wanda on 28 Nov 2011 00:21, edited 1 time in total.
-
highend
- Posts: 15016
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: how to combine items randomly?
Please provide a more detailed description...
Combine files is not the same as combining list entries from different files in (probably) a new one.
Are these real files (with extensions) or do you have e.g. two .txt files with <wx> number of lines with text in the first file and <yz> number of lines with text in the second one that should be combined in a new .txt file?
Combine files is not the same as combining list entries from different files in (probably) a new one.
Are these real files (with extensions) or do you have e.g. two .txt files with <wx> number of lines with text in the first file and <yz> number of lines with text in the second one that should be combined in a new .txt file?
One of my scripts helped you out? Please donate via Paypal
-
Wanda
- Posts: 55
- Joined: 08 Jan 2011 01:08
Re: how to combine items randomly?
listed items on text files that then will be copied into another text.
thanks for answering.
thanks for answering.
-
highend
- Posts: 15016
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: how to combine items randomly?
Script combines text lines from file a with randomized text lines from file b in a new file called c.
Edit paths and file names for your personal needs.
Example:
File ~a.txt:
File ~b.txt:
Result:
Edit paths and file names for your personal needs.
Code: Select all
/* 28.11.2011, Combine text entries from different files with randomization
::load "<xyscripts>\.snippets\CombineTextsFromFiles.xys";
*/
$FileName_a = "%temp%\~a.txt";
$FileName_b = "%temp%\~b.txt";
$FileDest = "%temp%\~result.txt";
$CombineText = " has ";
$FileContent_a = readfile("$FileName_a");
$FileContent_b = readfile("$FileName_b");
// Remove empty lines from both contents
$NoEmptyLines_a = regexreplace("$FileContent_a", "^\r?\n?", "");
$NoEmptyLines_b = regexreplace("$FileContent_b", "^\r?\n?", "");
// Replace line endings with "|"
$ContentWithSeparator_a = regexreplace("$NoEmptyLines_a", "[\r\n]+", "|");
$ContentWithSeparator_b = regexreplace("$NoEmptyLines_b", "[\r\n]+", "|");
// Count all "|" and substract 1
$Count = 1;
$Steps = gettoken($ContentWithSeparator_b, "count", "|") - 1;
// Randomize entries in file b
$RandomizedContent = "";
$Separator = "|";
while($Count <= $Steps){
$Random = rand(1, $Steps);
$Line = gettoken($ContentWithSeparator_b, $Random, "|");
$RandomizedContent = $RandomizedContent . $Line . "<crlf>";
$StringToReplace = $Line . $Separator;
$ContentWithSeparator_b = replace("$ContentWithSeparator_b", "$StringToReplace", "", 1, 1, 1);
$Steps--;
}
// Take each item from a and combine it with an item from randomized b
$NewContent = "";
$GetLine = 1;
foreach($Line, $ContentWithSeparator_a, "|"){
if($Line == ""){ break; }
$LineFrom_b = gettoken($RandomizedContent, $GetLine, "<crlf>");
if($LineFrom_b == ""){
$NewContent = $NewContent . $Line . "<crlf>";
} else {
$NewContent = $NewContent . $Line . $CombineText . $LineFrom_b . "<crlf>";
}
$GetLine++;
}
// Output the new content to destination file
writefile("$FileDest", $NewContent);
File ~a.txt:
Code: Select all
mutter
vater
sohn
enkel
tochter
geschwister
familie
onkel
Code: Select all
bild
stuhl
lehne
sofa
tisch
bett
Code: Select all
mutter has lehne
vater has stuhl
sohn has sofa
enkel has bett
tochter has bild
geschwister has tisch
familie
onkel
One of my scripts helped you out? Please donate via Paypal
-
Wanda
- Posts: 55
- Joined: 08 Jan 2011 01:08
Re: how to combine items randomly?
it's not working when both lists have the same amount of items can this be fixed?
thank you!
thank you!
-
highend
- Posts: 15016
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: how to combine items randomly?
I tried it with 8 different text lines in file a and b: no problems.
Both text files should have a trailing empty line (last line must be empty).
Both text files should have a trailing empty line (last line must be empty).
One of my scripts helped you out? Please donate via Paypal
-
Stefan
- Posts: 1360
- Joined: 18 Nov 2008 21:47
- Location: Europe
Re: how to combine items randomly?
Here's my try.
Note that i don't expect empty lines between content and at the end of the list!,
but one list may be longer then the other.
With a little bit of debug info:
$out = $out . "$i: " . "$outA ($randA)" . " has " . "$outB ($randB)" . "<crlf>";
And with even more randomness:
HTH
Note that i don't expect empty lines between content and at the end of the list!,
but one list may be longer then the other.
Code: Select all
$FileA = readfile("%temp%\~a.txt");
$FileB = readfile("%temp%\~b.txt");
$CountA = gettoken($FileA, "count", "<crlf>");
$CountB = gettoken($FileB, "count", "<crlf>");
if ($CountA <= $CountB){$Count = $CountB}else{$Count = $CountA};
$i=1; $out="";
while($i <= $Count){
$randA = rand(1, $CountA);
$randB = rand(1, $CountB);
$outA = gettoken($FileA, $randA, "<crlf>");
$outB = gettoken($FileB, $randB, "<crlf>");
if ($outA != "") and ($outB != ""){
$out = $out . $outA . " has " . $outB . "<crlf>";
$i++;
}
}
text $out;
//writefile("%temp%\~c.txt", $out);
With a little bit of debug info:
$out = $out . "$i: " . "$outA ($randA)" . " has " . "$outB ($randB)" . "<crlf>";
And with even more randomness:
Code: Select all
while($i <= $Count){
$randA = rand(1, $CountA);
$randB = rand(1, $CountB);
$randO = rand(1,2); //// FileA first in the Output, then FileB, or vise versa?
$outA = gettoken($FileA, $randA, "<crlf>");
$outB = gettoken($FileB, $randB, "<crlf>");
if ($outA !="") and ($outB !=""){
if ($randO == 1) {$out = $out . $outA . " has " . $outB . "<crlf>";}
if ($randO == 2) {$out = $out . $outB . " has " . $outA . "<crlf>";}
$i++;
}
}
HTH
-
Wanda
- Posts: 55
- Joined: 08 Jan 2011 01:08
Re: how to combine items randomly?
the absence of a trailing empty line was causing the problem highend. must keep that in mind. thank you very much!
your code is not preserving uniqueness of items stefan. sometimes it also produces this:
item a has photo 2
item a has photo 2
item c has photo 1
some of you can please take a look at my second request ie produce a list that forms matches from a single list with no repetition of names? "Fernanda and Fernanda" for instance.
your code is not preserving uniqueness of items stefan. sometimes it also produces this:
item a has photo 2
item a has photo 2
item c has photo 1
some of you can please take a look at my second request ie produce a list that forms matches from a single list with no repetition of names? "Fernanda and Fernanda" for instance.
-
admin
- Site Admin
- Posts: 67270
- Joined: 22 May 2004 16:48
- Location: Win8.1, Win10, Win11, all @100%
- Contact:
Re: how to combine items randomly?
I did not read the above carefully but FYI: the formatlist() function has a "shuffle" argument: x.
FAQ | XY News RSS | XY X
-
highend
- Posts: 15016
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: how to combine items randomly?
some of you can please take a look at my second request ie produce a list that forms matches from a single list with no repetition of names? "Fernanda and Fernanda" for instance.
Code: Select all
/* 28.11.2011, Combine text entries from the same file with randomized second half
::load "<xyscripts>\.snippets\CombineTextFromSameFile.xys";
*/
$FileName = "%temp%\~a.txt";
$FileDest = "%temp%\~result.txt";
$CombineText = " and ";
$FileContent = readfile("$FileName");
// Remove empty lines from both contents
$NoEmptyLines = regexreplace("$FileContent", "^\r?\n?", "");
// Replace line endings with "|"
$ContentWithSeparator = regexreplace("$NoEmptyLines", "[\r\n]+", "|");
// Count number of names in file
$Steps = gettoken($ContentWithSeparator, "count", "|") - 1; // all entries
$Modulo = $Steps % 2;
if($Modulo == 1){
$FirstHalf = ($Steps / 2) + 0.5;
} else {
$FirstHalf = $Steps / 2;
}
$RandomizedContent = "";
$Separator = "|";
$Count = 1;
while($Count <= $FirstHalf){
$Random = rand($FirstHalf + 1, $Steps);
$LineFromFirstHalf = gettoken($ContentWithSeparator, $Count, "|");
$RandomizedLine = gettoken($ContentWithSeparator, $Random, "|");
if($RandomizedLine != ""){
$RandomizedContent = $RandomizedContent . $LineFromFirstHalf . $CombineText . $RandomizedLine . "<crlf>";
} else {
$RandomizedContent = $RandomizedContent . $LineFromFirstHalf . "<crlf>";
}
$StringToReplace = $RandomizedLine . $Separator;
$ContentWithSeparator = replace("$ContentWithSeparator", "$StringToReplace", "", 1, 1, 1);
$Count++;
$Steps--;
}
// Output the new content to destination file
writefile("$FileDest", $RandomizedContent);
One of my scripts helped you out? Please donate via Paypal
-
Wanda
- Posts: 55
- Joined: 08 Jan 2011 01:08
Re: how to combine items randomly?
awesome! ^^
one last thing: how do i know the name of the person that was left out of the list if the list contains odd number of names?
one last thing: how do i know the name of the person that was left out of the list if the list contains odd number of names?
-
highend
- Posts: 15016
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: how to combine items randomly?
No entry is dropped.
If the list contains an odd number of entries, the last entry with no $CombineText output behind (in this case "and") is the one that has no "counterpart".
If the list contains an odd number of entries, the last entry with no $CombineText output behind (in this case "and") is the one that has no "counterpart".
One of my scripts helped you out? Please donate via Paypal
-
Wanda
- Posts: 55
- Joined: 08 Jan 2011 01:08
Re: how to combine items randomly?
but only if the last line of the list is empty as in the first case now i see. i'll have to remember that.highend wrote:No entry is dropped.
If the list contains an odd number of entries, the last entry with no $CombineText output behind (in this case "and") is the one that has no "counterpart".
-
highend
- Posts: 15016
- Joined: 06 Feb 2011 00:33
- Location: Win Server 2022 @100%
Re: how to combine items randomly?
You could add some code to prevent this.but only if the last line of the list is empty as in the first case now i see. i'll have to remember that.
Inspect the $ContentWithSeparator variable. If it doesn't end with a "|" there was no empty line at the end of the file. In that case, just add an "|" to the variable and you are done.
One of my scripts helped you out? Please donate via Paypal
XYplorer Beta Club