0

I have a large dataset of seasonal ratings for many pieces of equipment. Each piece of equipment currently has 3 data objects, in order: Summer>Winter>(Spring & Fall). I want to condense this so each piece of equipment only has one line of ratings.

$Equipment|%{if($_.C100_SP -match "Winter"){$aW=$_.A100_SU,$bW=$_.B100_SU,$cW=$_.C100_SU}elseif($_.C100_SP -match "Summer"){$aS=$_.A100_SU,$bS=$_.B100_SU,$cS=$_.C100_SU}elseif($_.C100_SP -match "Spring"){$_.A100_SP=$_.A100_F=$_.A100_SU;$_.B100_SP=$_.B100_F=$_.B100_SU;$_.C100_SP=$_.C100_F=$_.C100_SU;$_.A100_SU=$aS;$_.B100_SU=$bS;$_.C100_SU=$cS;$_.A100_W=$aW;$_.B100_W=$bW;$_.C100_W=$cW;$_.A='x'}}|Where-Object{$_.A -eq 'x'}

This code attempts to store the summer and winter ratings as variables ($aW, $bS, etc.), insert them into the spring record, then filter so only the filled out records are left. However, the winter and fall variables keep passing $null to the output, along with 9999, a common placeholder in this dataset.

A100_SP    : 4219
B100_SP    : 4219
C100_SP    : 9999
A100_SU    : {9999, $null}
B100_SU    : 
C100_SU    : 
A100_F     : 4219
B100_F     : 4219
C100_F     : 9999
A100_W     : {9999, $null}
B100_W     : 
C100_W     : 

The Spring and fall values are working, which makes me think this is a problem with storing variables between iterations, but I can't find much help on the web.

Any ideas how to get those variables to stay put?

0

1 Answer 1

1
  • The wrong statement separation in the first if clause: ,. It should be ;
  • Nothing was pushed down the pipeline: there should be $_ at the end of Spring case.
  • Instead of Where simply don't output anything to the pipeline in Winter and Summer .

$Equipment | %{
    switch -regex ($_.C100_SP) {
        "Winter" {
            $aW = $_.A100_SU
            $bW = $_.B100_SU
            $cW = $_.C100_SU
            break
        }
        "Summer" {
            $aS = $_.A100_SU
            $bS = $_.B100_SU
            $cS = $_.C100_SU
            break
        }
        "Spring" {
            $_.A100_SP = $_.A100_F = $_.A100_SU
            $_.B100_SP = $_.B100_F = $_.B100_SU
            $_.C100_SP = $_.C100_F = $_.C100_SU
            $_.A100_SU = $aS
            $_.B100_SU = $bS
            $_.C100_SU = $cS
            $_.A100_W = $aW
            $_.B100_W = $bW
            $_.C100_W = $cW
            $_
        }
    }
}

P.S. Don't use one-liners if you can't write them with your eyes closed :-)

Sign up to request clarification or add additional context in comments.

Comments

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.