locked
Assign values to array RRS feed

  • Question

  • Hello there,

    after sorting column "created" of table "$A" and assign them back to array "$A.created" I didn't get the output that I expected:

    $A=Import-Csv datum.csv
    $B=@()
    for ($i=0; $i -lt $A.Created.length; $i++) {
        $B+=[datetime]::parseExact($A.Created[$i],"dd.MM.yyyy HH:mm:ss",$null)
    }
    $C=$B | Sort-Object 
    for ($i=0; $i -lt $A.Created.length; $i++) {
        $A.Created[$i]=$C[$i].ToString("dd.MM.yyyy HH:mm:ss")
    }
    
    $A.Created[17]
    $C[17].ToString("dd.MM.yyyy HH:mm:ss")

    IMO the output should be the same and not:

    10.08.2018 09:22:27
    10.08.2018 08:54:27

    BTW: Why I can't assign $D to $A.created?

    $D=foreach($obj in $C){
         $obj.ToString("dd.MM.yyyy HH:mm:ss")
    } 
    #$A.Created=$D

    And is there a smarter way to get this results without loop?

    Here comes a sample of my csv:

    "Created"
    "26.06.2018 11:23:25"
    "05.07.2018 04:01:11"
    "06.07.2018 04:08:10"
    "06.07.2018 06:55:51"
    "09.07.2018 07:54:49"
    "12.07.2018 11:45:32"
    "13.07.2018 05:51:56"
    "13.07.2018 07:10:02"
    "19.07.2018 07:11:12"
    "19.07.2018 07:15:37"
    "27.07.2018 10:20:02"
    "30.07.2018 12:12:31"
    "06.08.2018 08:31:17"
    "06.08.2018 13:49:21"
    "08.08.2018 11:42:04"
    "10.08.2018 07:50:51"
    "10.08.2018 07:46:32"
    "10.08.2018 09:22:27"
    "10.08.2018 08:54:27"
    "14.08.2018 03:47:40"
    "15.08.2018 07:56:49"
    "16.08.2018 08:36:42"
    "16.08.2018 10:50:44"
    "17.08.2018 05:39:13"
    "17.08.2018 06:14:39"
    "20.08.2018 05:00:31"




    • Edited by Zero3000 Monday, December 17, 2018 5:07 PM
    Monday, December 17, 2018 4:44 PM

Answers

  • Could you explain that in a little more detail please, how to identify the structure of the object. For example why "$A.Created[$i]" works as expected in the following loop?


    for ($i=0; $i -lt $A.Created.length; $i++) {
        $B+=[datetime]::parseExact($A.Created[$i],"dd.MM.yyyy HH:mm:ss",$null)
    }



    This block "works" because $A.Created[$i] will give you the same overall output as $A[$i].Created.  The difference in the two is $A.Created is just a string while $A[$i] is an object with members/properties/index.

    $A[17] | Get-Member
    $A.Created | Get-Member
    
    

    Tuesday, December 18, 2018 4:01 PM

All replies

  • I think you could make your life easier ...
    Import-Csv -Path 'datum.csv' |
        ForEach-Object{
            [datetime]::parseExact($_.Created,"dd.MM.yyyy HH:mm:ss",$null)
        } |
            Sort-Object

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Monday, December 17, 2018 5:47 PM
  • You have the ordering of the property and index  reversed.  You are looking for the "created" property of the 18th object in $A ($A[17], and not the "created" property of just $A.

    $A[$i].Created=$C[$i].ToString("dd.MM.yyyy HH:mm:ss")
    
    $A[17].Created



    Monday, December 17, 2018 5:55 PM
  • Could you explain that in a little more detail please, how to identify the structure of the object. For example why "$A.Created[$i]" works as expected in the following loop?


    for ($i=0; $i -lt $A.Created.length; $i++) {
        $B+=[datetime]::parseExact($A.Created[$i],"dd.MM.yyyy HH:mm:ss",$null)
    }



    • Edited by Zero3000 Tuesday, December 18, 2018 11:39 AM
    Tuesday, December 18, 2018 9:00 AM
  • Could you explain that in a little more detail please, how to identify the structure of the object. For example why "$A.Created[$i]" works as expected in the following loop?


    for ($i=0; $i -lt $A.Created.length; $i++) {
        $B+=[datetime]::parseExact($A.Created[$i],"dd.MM.yyyy HH:mm:ss",$null)
    }



    This block "works" because $A.Created[$i] will give you the same overall output as $A[$i].Created.  The difference in the two is $A.Created is just a string while $A[$i] is an object with members/properties/index.

    $A[17] | Get-Member
    $A.Created | Get-Member
    
    

    Tuesday, December 18, 2018 4:01 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, December 24, 2018 2:51 AM