oneliner - clean hash with getenumerator

Brass Contributor

Hi,

 

is there any way to do this in a oneliner manner ?

 

#Removing records older than 365 days
$tmp = $IPRouteInformation.clone()
foreach($k in $tmp.GetEnumerator()){
    if($k.Value.TimeID -lt (date (get-date).AddDays(-365) -Format yyyyMMddhhmmssfff)){
        $IPRouteInformation.Remove($k.Key)
    }
}

 

 

I tried different things but seems like get-enumerator is blocking me. The code bellow does nothing.

 

$IPRouteInformation.count
$IPRouteInformation.GetEnumerator() | ? { $_.Value.TimeID -lt (date (get-date).AddDays(-1) -Format yyyyMMddhhmmssfff) } | % {$IPRouteInformation.Remove($_)}
$IPRouteInformation.count

 

 

Thank you for your help.

2 Replies

@John_Dodo 

 

Hashtables aren't great for this kind of thing. It'd be easier and far more efficient with a list than a dictionary, but I'll still speak to the request above.

 

As you've already noted, one option is to call .Clone() and then look to use .Remove() to remove items you don't. The problem with this approach is it doesn't scale well since you have to perform a full duplication of the original list's references (i.e. a shallow copy.) If the original list is very large, this can prove quite inefficient.

 

Conversely, if you expect that the final list will be roughly the same size as the initial list (i.e. you're not filtering out all that much) then any efficiency gains you might achieve using another approach are offset by how easy using .Clone() makes things.

 

However, if you're expecting the final list to be much smaller (a subjective statement, but let's say less than half), then you'd be better off creating an empty second hashtable and calling the .Add() method on it to add just the records you wish to keep. This approach avoids a full duplication of the original list.

 

Neither approach is as efficient as using a list but it illustrates that there are options even within the hashtable context.

 

# Example 1: Growing a new list (potentially more efficient - depends on size of original collection.)
$FinalList = @{};
$Ticks = [datetime]::Now.AddDays(-365).Ticks;
$IPRouteInformation.GetEnumerator() | ForEach-Object { if ($_.Value.TimeID.Ticks -ge $Ticks) { $FinalList.Add($_); } }

# Example 2: Fully-duplicating the original list then removing entries (i.e. shrinking the new list).
$Ticks = [datetime]::Now.AddDays(-365).Ticks;
$IPRouteInformation.Clone().GetEnumerator() | ForEach-Object { if ($_.Value.TimeID.Ticks -lt $Ticks) { $IPRouteInformation.Remove($_.Key); } }

 

I've assumed from your code that "TimeID" is a [datetime] object. If it's a string, let me know and I'll change my examples to suit if you need me to.

 

Cheers,

Lain

@LainRobertson thank you very much, very useful as usual.

 

I will review these lines but I already know that it's going to work.

 

Finally what I really understand is that I need to learn what a list is (never used it) and how it compares to arrays and hashtables, and consequently, when we do need arrays, as hastables and/or list seem to be much more convenient.

Also I quite often do hash of hashes variables. I wonder if the right answer is not a list of hashes. To be studied...

 

Thank you !!