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.
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.
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?
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?
Let’s do it step by step. First - read the file and create a collection of all the words in it.
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?
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.
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.
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.
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
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. That’ll save you from a lot of wasted time and frustration.
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.