Author Posts

April 8, 2017 at 12:31 am

I have a very long string I need to split in several places.
Example

1. Distraction — Target suffers a -30 modification for all
actions.
2. Confusion — Target is incapable of making decisions or
initiating action. He may continue to fight current foes or in
self-defense with +5.
3. Blur Vision — Target suffers a -100 OB modification for
missile attacks, and a -50 modification for all other actions.
4. Fear — Target fears caster and attempts to flee. Fleeing
normally equates to moving
at maximum pace away from
the caster.
5. Stumble — Target becomes
unbalanced. If he is moving,
he trips and falls (0% action
for 1-5 rnds). If he is performing
a maneuver, the
manuever fails.

I am trying to split this at each 1,2,3,4,5 but the rows ending with +5. cause a lot of problems. Any ideas?

Thanks in advance.

April 8, 2017 at 1:05 am

$myString = '1. Distraction — Target suffers a -30 modification for all
actions.
2. Confusion — Target is incapable of making decisions or
initiating action. He may continue to fight current foes or in
self-defense with +5.
3. Blur Vision — Target suffers a -100 OB modification for
missile attacks, and a -50 modification for all other actions.
4. Fear — Target fears caster and attempts to flee. Fleeing
normally equates to moving
at maximum pace away from
the caster.
5. Stumble — Target becomes
unbalanced. If he is moving,
he trips and falls (0% action
for 1-5 rnds). If he is performing
a maneuver, the
manuever fails.'

$myOutput = 1..5 | % { 
    $StartMarker = $myString.IndexOf("$_. ")
    if ($_ -eq 5) {
        $EndMarker = $myString.Length
    } else {
        $EndMarker = $myString.IndexOf("$($_+1). ")
    }  
    [PSCustomObject]@{
        Number = $_
        String = $myString.Substring($StartMarker+3,$EndMarker-(3+$StartMarker))
    }
}

$myOutput | FL

Output:

Number : 1
String : Distraction — Target suffers a -30 modification for all
         actions.
         

Number : 2
String : Confusion — Target is incapable of making decisions or
         initiating action. He may continue to fight current foes or in
         self-defense with +5.
         

Number : 3
String : Blur Vision — Target suffers a -100 OB modification for
         missile attacks, and a -50 modification for all other actions.
         

Number : 4
String : Fear — Target fears caster and attempts to flee. Fleeing
         normally equates to moving
         at maximum pace away from
         the caster.
         

Number : 5
String : Stumble — Target becomes
         unbalanced. If he is moving,
         he trips and falls (0% action
         for 1-5 rnds). If he is performing
         a maneuver, the
         manuever fails.

April 8, 2017 at 3:01 am

There are many ways to do this. Here is one more:


# Mocked Get-Content, so the test input can live in one file.
function Get-Content-ment {
@'
1. Distraction — Target suffers a -30 modification for all
actions.
2. Confusion — Target is incapable of making decisions or
initiating action. He may continue to fight current foes or in
self-defense with +5.
3. Blur Vision — Target suffers a -100 OB modification for
missile attacks, and a -50 modification for all other actions.
4. Fear — Target fears caster and attempts to flee. Fleeing
normally equates to moving
at maximum pace away from
the caster.
5. Stumble — Target becomes
unbalanced. If he is moving,
he trips and falls (0% action
for 1-5 rnds). If he is performing
a maneuver, the
manuever fails.
'@ -split '\r?\n'
}


function Parse-File {
    param (
        $Path
    )

    $headingRegex = '^(\d+)\.\s(.*)'
 
    $output = New-Object System.Text.StringBuilder

    # Change Get-Content-ment to Get-Content for this to work correctly.
    foreach ($line in (Get-Content-ment $Path)) {
        if ($line -match $headingRegex) {
            if ($output.Length -gt 0) {
                Write-Output $output.ToString()
                $null = $output.Clear()
            }
            $null = $output.AppendLine($Matches[2])
        }
        else {
            $null = $output.AppendLine($line)
        }
    }
    if ($output.Length -gt 0) {
                Write-Output $output.ToString()
    }
}

$bulletPoints = Parse-File U:\temp\temp.txt

$bulletPoints.Count

$bulletPoints


Which outputs:

5

Distraction — Target suffers a -30 modification for all
actions.

Confusion — Target is incapable of making decisions or
initiating action. He may continue to fight current foes or in
self-defense with +5.

Blur Vision — Target suffers a -100 OB modification for
missile attacks, and a -50 modification for all other actions.

Fear — Target fears caster and attempts to flee. Fleeing
normally equates to moving
at maximum pace away from
the caster.

Stumble — Target becomes
unbalanced. If he is moving,
he trips and falls (0% action
for 1-5 rnds). If he is performing
a maneuver, the
manuever fails.

Did you intend there to be, carriage returns within the bullet points (numbered blocks)? As, this would be much simpler without them.

April 8, 2017 at 10:05 am

Thank you so much for such fast and well written answers. Why is everything so simple when you see an answer!