RJ96
RJ96

Reputation: 313

Unique name combos out of 2 files

I know similar questions have been asked quite a few times before but in all the research i did online all i was able to find was questions about getting unique combos from a single array and so on but that i can do but what i'm looking for is a bit different.

I have a script that generates names by getting firstnames from a txt.file and surnames from another and then randomly creates a name, the script generates duplicates as well, so i was wondering, how can i get the number of all the possible unique combos? Considering that each text.file has 20 items (lastnames file has 20 items.

this is my script:

$FirstName = Import-Csv C:\Users\myuser\Desktop\Firstname.txt -Delimiter ';' -Header Name, Gender
$LastName = Get-Content  C:\Users\myuser\Desktop\Lastnames.txt
[array]$FirstNameMan = $null
[array]$FirstNameWoman = $null



[int]$NumberOfCombinations = Read-Host 'Enter the number of combinations you wish'
[string]$Gender = Read-Host 'Gender: Enter "M" for "man" or "W" for "woman"'

# here i split the names by gender
for ($i = 0; $i -lt $FirstName.Count; $i++) {
if ($FirstName[$i].Gender -like 'mann') {
    $FirstNameMan += $FirstName[$i].Name
}
else {
    $FirstNameWoman += $FirstName[$i].Name
}
}
#random join
  For ($j = 0; $j -lt $NumberOfCombinations; $j++) {
if ($Gender -like 'm') {
    $FirstNameMan[(Get-Random $FirstNameMan.Count)] + ', ' + $Lastname[(Get-Random $Lastname.Count)] 
}
else { 
    $FirstNameWoman[(Get-Random $FirstNameWoman.Count)] + ', ' + $Lastname[(Get-Random $Lastname.Count)] 
}
}

the result would be something like this(based on the chosen gender):

Katja, Schult
Mary, Krüger
Ruth, Meier
Ruth, Bauer
Katja, Bauer
Katja, Schneider
Mascha, Möller
Ruth, Schröder
Sara, Meier

now how can i find out how many possible unique combinations one could have? (not unique would be when you get the same name combo twice, otherwise it is unique) i think i just can't seem to find the logic to figure out an algorithm

sorry if this is too subjective

Upvotes: 0

Views: 37

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174525

Generating all unique combinations of two unique lists is as easy as writing a nested loop:

$AllMaleNames = foreach($first in $FirstNameMan){
    foreach($last in $Lastname){
        "$first, $last"
    }
}

$AllFemaleNames = foreach($first in $FirstNameWoman){
    foreach($last in $Lastname){
        "$first, $last"
    }
}

Now you can pick from those lists instead:

if($Gender -like 'm') {
  $NamesToPickFrom = $AllMaleNames
}
else {
  $NamesToPickFrom = $AllFemaleNames
}

$NamesToPickFrom | Get-Random -Count $NumberOfCombinations

Upvotes: 1

Related Questions