Fruit Basket Problem

Problem:
I have a list of objects, and I want to use each of them to do something. When that part is complete, I want the object removed from the list. There maybe multiple instances of the same named object.

For example my list may be like this:

Apple,Apple,Banana,Apple,Orange,Tomato,Apple,Pear,Orange

or

Apple,3
Banana,2
Orange,2
Pear,1
Tomato,1

How can this be achieved with Powershell?
I have looked into lists and dynamic m/d arrays

What is best?

Many thanks

With Powershell it’s really easy to iterate over a given “collection”, no matter if it is an array or a hashtable or whatever. Simply use Foreach. And if I got you right you don’t need to “remove” single elements. If you finished iterating over the collection you could simply delete it or recreate it empty.

Perfect!

Do you have an example that I could follow?

  • Phil

The linked help article has some examples. Did you have at least a short look at it? :-/

I suppose I need to provide some extra information or context.

I have have spent many hours today working through this.

I have used different types arrays / collections

I can iterate through the collection, but cannot seem to select or remove a specific object out of the array or reduce the count of the object type.

The fruit basket is not the real world problem I’m dealing with, but the easiest way I could explain it.

I thought if I could get a nudge on the right direction I could work through the issue.

I really do appreciate any help

  • Phil

Example:

$FruitList = @('Apple','Apple','Banana','Apple','Orange','Tomato','Apple','Pear','Orange')
$IndexList = @()

# Process 1
$Index = 0
foreach ($Fruit in $FruitList) {
    
    # Process Apple's
    if ($FruitList[$Index] -eq 'Apple') {
        "Processing '$Fruit' - element # '$Index' of the FruitList array"
        $IndexList += $Index 
    }

    $Index ++

}

# Remove processed elements by recreating the FruitList array
$Index = 0
$FruitList = $FruitList | foreach {
    if ($Index -notin $IndexList) { $FruitList[$Index] }
    $Index ++
}
'Now FruitList is:'
$FruitList

Also see this post

Sam - This is great stuff!

I think this example is great starting point for me. also, that blog post looks like it has some other stuff i can utilise.

Many thanks for your help!

  • Phil
I can iterate through the collection, but cannot seem to select or remove a specific object out of the array or reduce the count of the object type.
I tried to motivate you to re-think the approach you wanted to achive. Is it really necessary to remove single elements with each loop pass if you have a reliable and easy way to simply iterate over all elements of the collection one at a time? ;-) ;-)

I’m interested in what you mean.

My current thought process is that I need to iterate through the collection one at a time. Do some stuff with it and if I needed to look back into the basket to see what’s left. I was getting stuck on having multiple counts of the same object.

If you have better way I’m very interested in how.

Many thanks

  • Phil

Yep, you build your decision inside the loop based on BOTH the value and the array index number as in:

$FruitList = @('Apple','Apple','Banana','Apple','Orange','Tomato','Apple','Pear','Orange')
$Index = 0
$Value = 'Apple'
foreach ($Fruit in $FruitList) {
    if ($FruitList[$Index] -eq $Value) {
        "I know this is '$Value' - but not just any $Value, this is my #'$Index' array element"
    }
    $Index ++
}

In other words, if you want to process ‘Apple’ members of your array, doing a match based on value is not enough since value is not unique. The element index number is unique and can help you address each ‘Apple’ distinctively…

... if I needed to look back into the basket to see what's left ...
That's what I mean. ;-) Do you really need to look back? Whatfor? Why? What is the benefit of looking back? It just takes a lot of effort. I used to think it's not necessary.