0

I tried to split below array into multiple smaller array using PowerShell. But it takes much time to output the text file line by line. Is there other way to improve the performance?

Original array ($FRUITARRAY):

ORANGE,1,4,5,6,7,8
APPLE,1,3,2,4,5
ORANGE,2,4,5,6,7,8
ORANGE,3,4,5,6,7,8
APPLE,2,3,2,4,5
ORANGE,4,4,5,6,7,8
ORANGE,5,4,5,6,7,8
APPLE,3,3,2,4,5
APPLE,4,3,2,4,5

Desired output arrays:

$ORANGERRAY:

ORANGE,1,4,5,6,7,8
ORANGE,2,4,5,6,7,8
ORANGE,3,4,5,6,7,8
ORANGE,4,4,5,6,7,8
ORANGE,5,4,5,6,7,8

$APPLEARRAY:

APPLE,1,3,2,4,5
APPLE,2,3,2,4,5
APPLE,3,3,2,4,5
APPLE,4,3,2,4,5

Code:

$FRUITARRAY = Get-Content 'D:\work\fruit.txt'

for ($i=1; $i -lt $FRUITARRAY.Length; $i++) {
    if ($FRUITARRAY[$i].Split(',')[0].StartsWith('ORANGE')) {
        $FRUITARRAY[$i] | Out-File "D:\work\ORANGE.TXT" -Append
    }

    if ($FRUITARRAY[$i].Split(',')[0].StartsWith('APPLE')) {
        $FRUITARRAY[$i] | Out-File "D:\work\APPLE.TXT" -Append
    }
}

$ORANGEARRAY = Get-Content "D:\work\ORANGE.TXT"
$APPLEARRAY  = Get-Content "D:\work\APPLE.TXT"
3
  • I tried to re-format your code, but it's too broken to be salvaged (it's not valid powershell). Please update the post with the actual code that's currently running slow Commented Jul 15, 2019 at 16:21
  • Edited the actual code. There could be 500k lines in the text file.Thanks... Commented Jul 15, 2019 at 16:38
  • If you do not require the files (ORANGE.TXT and APPLE.TXT), I would highly recommend to keep everything in memory. You might just concat an array by first assigning an empty array:` $ORANGEARRAY = @()` and than add the new values like: if ($FRUITARRAY[$i].StartsWith('ORANGE')) {$ORANGEARRAY += $FRUITARRAY[$i]}. (btw., there is no use spiting it if you use .StartsWith) Commented Jul 15, 2019 at 16:49

1 Answer 1

1

Arguably the most elegant solution would be Group-Object:

$fruits = Get-Content 'D:\work\fruit.txt' | Group-Object { $_.Split(',')[0] }

$apples  = $fruits |
           Where-Object { $_.Name -eq 'apple' } |
           Select-Object -Expand Group
$oranges = $fruits |
           Where-Object { $_.Name -eq 'orange' } |
           Select-Object -Expand Group
Sign up to request clarification or add additional context in comments.

2 Comments

Why not use the name of the group to name the variable? foreach($fruit in $fruits){Set-Variable -Name "$($fruit.Name)Array" -Value ($fruit.Group)} provided the actual names are valid as variable names.
@LotPings I consider dynamically named variables a bad practice, so I will not recommend doing that. If anything, put the groups in a hashtable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.