locked
How can I clear the table that's created by importing a csv file? RRS feed

  • Question

  • Hi,

    This is probably very simple, but I can't find a way to clear the table that has been created by importing a csv file into a variable. 

    This is how my code looks like:

    $data = Import-CSV [name] ForEach($var in $data){ $a += $var.a $b += $var.b $c += $var.c echo $c; }


    Now each time I run the script, I can see the values for c being added to the variables table. I need this to run once and then have the table cleared.I tried with:

    - remove-variable $data

    - remove-variable  -name "data"

    - clear variable $data

    And can't get anything to work:(

    Thanks,

    Wojciech



    • Edited by rozanw Tuesday, February 13, 2018 12:10 PM
    Tuesday, February 13, 2018 12:07 PM

Answers

  • Wrong expression. Changing += to = was all that was needed. Simple mistake.

    Thanks,

    Wojtek

    • Marked as answer by rozanw Wednesday, February 14, 2018 9:37 AM
    Wednesday, February 14, 2018 9:37 AM

All replies

  • There is no table here.  A CSV is an array of custom objects.

    To reload the same variable just reassign it in another import statement.  There is no need to empty it.

    If you want to clear the array just use "Clear"

    $data.Clear()

    Although it serves no purpose here.


    \_(ツ)_/

    Tuesday, February 13, 2018 12:27 PM
  • The purpose is purely for testing. To test a sample of the code I need to empty the array after the script is run. 

    Still can't get this to work though. Each time I run the script the echo output shows what was before + additional values of c.

    It's a little weir though. I have only 2 values in the c column in the CSV file, and currently:

    - echo displays 50+ values

    - data.count shows 2

    So it does look like everything is in order, but how an I stop echo from displaying so may lines?

    Thanks,

    Wojciech


    Tuesday, February 13, 2018 12:56 PM
  • $a,$b,$c are not columns they are just variables.

    Nothing is being changed in the CSV array. You are just copying all of the columns into variables.

    As I noted.  It makes no sense.  The code does nothing useful.

    I think you need to learn some very basic programming before attempting to do experiments.  Learn what variables, arrays and CSV files are and how they are managed.

     


    \_(ツ)_/

    Tuesday, February 13, 2018 1:08 PM
  • I am not trying to change anything in the CSV array. I just need to read data from it, one line at a time, and operate on that data further in the script. 


    Tuesday, February 13, 2018 1:34 PM
  • Why does that require you to add the data from all rows into variables?  Why does that require you to clear the array?

    What you are asking and the code yu have posted don't match.  It is impossible to know what your purpose is.


    \_(ツ)_/

    Tuesday, February 13, 2018 1:40 PM
  • Ok, figured out where the problem was. 

    Thanks,

    Wojciech

    • Marked as answer by rozanw Tuesday, February 13, 2018 1:43 PM
    • Unmarked as answer by jrv Tuesday, February 13, 2018 5:10 PM
    Tuesday, February 13, 2018 1:43 PM
  • Hi Rozanw,

    please feel free to update this thread with your fix as someone may find it useful in future :)

    Tuesday, February 13, 2018 3:00 PM
  • If you have an answer then post it and mark the post as the answer.  Marking a post that says "I got it" is not very helpful to anyone.


    \_(ツ)_/

    Tuesday, February 13, 2018 5:11 PM
  • Wrong expression. Changing += to = was all that was needed. Simple mistake.

    Thanks,

    Wojtek

    • Marked as answer by rozanw Wednesday, February 14, 2018 9:37 AM
    Wednesday, February 14, 2018 9:37 AM
  • That has nothing to do with the question you asked.  Assigning one variable in a loop serves no useful purpose.

    Asking to clear the CSV does not indicate that the issue was not with the CSV.  Start by learning basic PowerShell to prevent future confusion.


    \_(ツ)_/

    Wednesday, February 14, 2018 9:53 AM