• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How to split a 2-D array?

SONYFX

Senior member

For example when do a database query I have 3 columns and 4 rows returned as String[][] :


ID, CITY, COUNTRY
------------------------
1, NY, USA
2, NY, USA
3, NY, USA
4, HK, CHINA


How do I split the returned array into two separate String[][] based on COUNTRY? i.e. one array for USA and one array for CHINA. (I don't want to do two database queries)
 
china = string[]
us = string[]
for city in cities:
__if (city[2] = 'USA') us.add(city)
__else china.add(city)

If you want to handle an arbitrary number of countries, instead of two arrays, use one dictionary of arrays. Alternatively, if you don't need individual city info (like you were only going for averages or something like that) you could look into a 'group by' query.
 
Originally posted by: kamper
china = string[]
us = string[]
for city in cities:
__if (city[2] = 'USA') us.add(city)
__else china.add(city)

If you want to handle an arbitrary number of countries, instead of two arrays, use one dictionary of arrays. Alternatively, if you don't need individual city info (like you were only going for averages or something like that) you could look into a 'group by' query.

Thanks but I need to store the "whole row" in 2-D array.
 
That's what my code does although my typing wasn't particularly clear (it's just pseudo-code after all). 'china' and 'us' should be string[][].
 
Back
Top