Integrate do .. until looping in Powershell

hello. My Powershell code must mix all words from file.txt My Replace doesn’t change too many words between them I believe I must Integrate a do .. until looping code. I find this LINK and I try many combinations, but ruins all my code.

$fileName = "C:\Folder1\file.txt"
  $newfilename = "C:\Folder1\newfile.txt"
  # Get content of file
  $content = Get-Content -Path $fileName -Raw -Encoding UTF8
  $words = (((($content.Split(" ")).Replace(".","")).Replace(",","")).Replace("`n",""))
  #Define function
  Function Create-Words {
      $script:word1 = Get-Random -InputObject $words 
      $script:word2 = Get-Random -InputObject $words  
      Write-Host "First word: " $script:word1 -ForegroundColor Red # output word1
      Write-Host "Second word: " $script:word2 -ForegroundColor Cyan # output word2
  }
  # Execute function
  Create-Words
  # Replace words randomly
  $content.Replace("$word1", "$word2") | Out-File -FilePath "$newfilename" -force

Is this a homework question?

something like that. I want to understand how can be integrate that function on a code. I’m not a developer. Step by step, optional.

I’d start with removing the line numbers and all backtricks from the code. At the moment it would be a pain in the ass to copy your code and play with it. You force people willing to help you to make more effort than necessary. :wink:

1 Like

Of course. Done ! @Olaf

1 Like

Cool. Thanks.

Just that I get it right - you want to take all the words from a text file and mix them up in a random order, right?

@Olaf yes, exactly, it doesn’t matter how many words I have in the file. They must be as mixed as possible, like a very difficult puzzle :slight_smile:

OK, I got it. :wink:

Let’s try something. First I’m going to describe how I would approach it. You can try it yourself and if you get stuck with the code we do the code together. OK? :slight_smile:

Since you actually want to create a new file anyway I’d drop all that replacing stuff.

Basically we need all the words in the file in a collection where we can randomly pick one of the words at a time, remove it from the source collection and add it to a new collection. We have to repeat this until all source words have been used.

If we have all the words in a new order in our new collection we can write it to a file. Simple, isn’t it? :wink:

Let’s do it step by step. First - read the file and create a collection of all the words in it.

1 Like

OK, just to be clear, if this a college assignment or homework question we try avoid assisting with those as the first port of call should be your tutor. However, you’ve had a decent crack at it with your code so some general observations:

It would be good to know what the input file looks like and what the expected output is. If it’s just a list of words, that’s super simple to solve. If it’s properly structured text (and your -split and -replace operations suggest that might be the case) then it’s a harder problem, particularly if you want to maintain some structure in the new file. What’s going to happen to all the a’s and i’s when you do a replace? What about words that appear more than once, do you want to replace both instances or just the first match? :thinking:

So, if it’s just a list of words you won’t need a loop. Just take the list, sort it randomly (that’s a hint) and output it.

If it’s structured text and you want to maintain the structure, you probably will need a loop and your replace operations will need more complicated regular expressions.

2 Likes

I didn’t know that. :astonished: Very cool. :+1:t4: Thanks. :kissing_heart: :vulcan_salute:t4:

thank you @Olaf and @matt-bloomfield

You both write literature. Not even a line of code. And just for future reference, you need to know that a man learns from concrete examples, not from words full of wisdom. Whoever talks to you a lot and doesn’t show you anything concrete, is the same man who doesn’t teach you anything.

If you had found the solution, it was easiest to write it down, and I would have learned how to do it.

I totally disagree.

And I don’t like to do other peoples homework on request. :wink:

Regardless of that is it still unclear what exactly you actually want to do.

1 Like

We can’t post code as a solution if we don’t know what the problem is.
Show us what the input looks like and what the output should look like and we can help you work out a solution.

1 Like

@matt-bloomfield @Olaf

For example I have a text with 400 words, but I show you a short example with 7 words.

The input: ( file.txt )

We can’t post code as a solution if we don’t know what the problem is.

The output: ( newfile.txt )

as what the problem a solution we is. post if can’t We code know don’t

OK, so although the input is not a list of words, you’re not maintaining the structure. In that case, I would suggest that you treat punctuation as part of the word and don’t bother stripping it out. You can then just treat all your words as though it is a list which can be randomized and then joined together with spaces. No loop necessary:

$sentence = "We can't post code as a solution if we don't know what the problem is."
$words = $sentence -split ' '
$newSentence = ($words | Sort-Object {Get-Random}) -join ' '
Write-Output $newSentence
2 Likes

That should have been in your initial question. :wink:

((Get-Content -Path .\File.txt -Raw ) -split "\s+" | 
    Sort-Object {Get-Random} ) -join ' ' |
        Out-File -FilePath .\NewFile.txt
1 Like

yes, @matt-bloomfield , but I need to select the words from the file, not implicit on powershell :slight_smile:

@Olaf I put your code on the beginning. Doesn’t change almost nothing in the Output.

((Get-Content -Path C:\Folder1\file.txt -Raw ) -split "\s+" | 
    Sort-Object {Get-Random} ) -join ' ' |
        Out-File -FilePath C:\Folder1\NewFile.txt

	# Get content of file

  $content = Get-Content -Path $fileName -Raw -Encoding UTF8
  $words = (((($content.Split(" ")).Replace(".","")).Replace(",","")).Replace("`n",""))
  #Define function
  Function Create-Words {
      $script:word1 = Get-Random -InputObject $words 
      $script:word2 = Get-Random -InputObject $words  
      Write-Host "First word: " $script:word1 -ForegroundColor Red # output word1
      Write-Host "Second word: " $script:word2 -ForegroundColor Cyan # output word2
  }
  # Execute function
  Create-Words
  # Replace words randomly
  $content.Replace("$word1", "$word2") | Out-File -FilePath "$newfilename" -force

We cannot help you when you don’t understand the help. With the given example text in the source file my code does exactly what you described. You don’t need more code.

I’d recommend to do a big step back and start with learning the very basics of PowerShell first. :wink: That’ll save you from a lot of wasted time and frustration.

2 Likes

Sorry, I’m not going to provide a full code solution for a homework question. You now have all the building blocks you need to get this working, including a one-liner solution from @Olaf.

From your original post, you already know how to read and write to a file. So to modify my example you just need to replace the first and last lines of my example with the Get-Content/Set-Content commands.

2 Likes