Re: Create two-dimensional Array from string
- From: "pjfalbe@xxxxxxxxx" <pjfalbe@xxxxxxxxx>
- Date: Fri, 4 Apr 2008 06:32:58 -0700 (PDT)
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.
.
- Follow-Ups:
- Re: Create two-dimensional Array from string
- From: Ted Zlatanov
- Re: Create two-dimensional Array from string
- References:
- Create two-dimensional Array from string
- From: pjfalbe@xxxxxxxxx
- Re: Create two-dimensional Array from string
- From: jjcassidy
- Create two-dimensional Array from string
- Prev by Date: Re: Creating a 'load simulator' by calling Perl Programs - or Forking?
- Next by Date: separating substitutions from an embedded perl in a ksh script
- Previous by thread: Re: Create two-dimensional Array from string
- Next by thread: Re: Create two-dimensional Array from string
- Index(es):