Re: Create two-dimensional Array from string



On Apr 3, 10:49 am, jjcass...@xxxxxxxxx wrote:
On Apr 2, 1:21 pm, "pjfa...@xxxxxxxxx" <pjfa...@xxxxxxxxx> wrote:

I'm looking for the most efficient way to create a two dimensional
array from strings such as

MULTISET{ROW(1 ,' 3667 ','WARREN ','OH'),ROW(2 ,' 2948
','SHAKER HTS','OH')}

In this case MULTISET is a collection of rows from a DB. What I want
todo is efficiently
transform into a 2-dimensional array, where each row is an array.
And then collect those arrays
into 1 array of arrays. I'm currently doing this but not very
efficiently. Any help would be appreciated.

Let Perl parse it for you. You can *eval* it.

use Data::Dumper;

sub ROW (@) { return [ @_ ]; }

sub MULTISET (&) { return [ sort { $a->[0] <=> $b->[0] } shift()-

() ]; }

# observe:
my $multi_set = eval <<END_EVAL1;
MULTISET{ROW(1 ,' 3667 ','WARREN ','OH'),ROW(2 ,'
2948','SHAKER HTS','OH')}
END_EVAL1

print Dumper( $multi_set ), "\n";

You could even define the two functions as below if you simply wanted
the first field for ordering.


This works great! Much better than all the sed'ing and splitting I
was doing. For reference I'm doing a query like

select customer, multiset(select count(*), city, state
from orders A
where A.customer = orders.customer
and A.order_date = orders.order_date
group by 2,3)
from orders
where order_date = today;


With your transform I can now take the field returned from the
multiset and write to WriteExcel spread*** very easily.

.