0

I am attempting to create a script to read a CSV, then perform some operations on the contents where the first field are similar. Right now I'm stuck on trying to set up the second While loop to compare the current element to the next one.

I'm fairly new to this, because I wasn't getting anywhere trying this in Java. I can't seem to find a combination of commands that will let the loop work.

Some things I've tried are:

While($csv[$count].ip -eq $csv[$count++].ip)

While((diff $csv[count].ip $csv[$count++].ip) = true)

While($csv[$count].ip = $csv[$count++].ip)

4
  • 1
    Any code to show? It would help. Commented Feb 27, 2018 at 19:08
  • Tough to tell you what you are doing wrong if we don't know what you are doing... Commented Feb 27, 2018 at 19:16
  • Just updated my main message with some examples. Commented Feb 27, 2018 at 19:35
  • Update your question with a few sample CSV lines and explain what you are trying to compare and why. Make it possible for readers to precisely reproduce your problem. Commented Feb 27, 2018 at 19:40

1 Answer 1

1

Don't use $count++ unless you want to actually change the value of $count itself. Instead use $count + 1 as the array index

$count = 0
while($count -le $csv.Count){
    if($csv[$count].ip -eq $csv[$count + 1].ip){
        # Do your stuff here
    }
    $count++
}
Sign up to request clarification or add additional context in comments.

2 Comments

That solves that, but it looks like my code is ignoring variables from inside the loop even if I add script: or global:
@Binsky734 can you post more of the code? I might be able to help

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.