Update every value in a multivalue array with a specific value RRS feed

  • Question

  • I have a large array (sometimes in the order of tens (and even hundreds) of thousands of records)

    for example: (this isn't what I have but the idea is exactly the same)

    (item, country, price)
    (item, country, price) 

    I have added a tax column so now there is

    item, country, price, tax for each record

    but tax is null.  What I want to do, is on every item in a specific country, I want to set the tax consistently.  But I don't want to parse through the entries.

    so instead of

    foreach ($record in $array|where {$_.Country -eq "Australia"}){$record.tax = "10%"}

    is there a way I can just do the setting without having to do the foreach parse?  When you are dealing with tens of thousands of records (sometimes more), this takes a fair amount of time.

    • Moved by Bill_Stewart Tuesday, November 7, 2017 9:39 PM Unanswerable drive-by question
    Tuesday, September 12, 2017 4:51 AM

All replies

  • No.  You have to assign each one individually.  A loop is the easiest way to do this.  There is no magic trick that can guess where to put this.

    You could create a file of countries and taxes and use that to enumerate the array.  

    An array of even 100,000 items can be enumerated in a couple of seconds so time should be no issue. The big question to answer is where are you getting the tax rate from?

    For this kind of thing you should consider using a database.  A DB is designed to efficiently manipulate this kind of data with no programming.


    Tuesday, September 12, 2017 5:24 AM
  • It's not a tax thing, that was an example.....

    It's actually involving IP addresses and subnets and the countries for those IP addresses.  I was trying to make the example as simplified as possible because I have all the complex guts of this worked out (using hash tables etc), just the ability to quickly update all of the items in an array based on the subnet found with the associated country that is recorded in the hash table.  A database isn't really appropriate because of how this is generated and what it's used for.

    But thanks.... if there's no way to mass update a column in an array, I will just have to live with the foreach parse...

    Wednesday, September 13, 2017 12:14 AM
  • What, inyour mind, constitute  a mass update?  That is not a technical term.  A "mass" anything is a term used in news reports and is usually referenced to "mass" murders or "mass" burials?

    In computer technology we talk about batch updates which means enumerating something and applying a value or calculated change to the enumerated elements.

    There is no such thing as "mass programming".


    Wednesday, September 13, 2017 12:48 AM