Re: Create two-dimensional Array from string



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.

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

sub MULTISET (&) {
my %hash = ( shift()->() );
return [ map { $hash{$_} } sort { $a <=> $b } keys %hash ];
}

or

sub MULTISET ($) { # for {} as a hash ref
my $hash_ref = shift;
return [ map { $hash_ref->{$_} } sort { $a <=> $b } keys %
$hash_ref ];
}
.



Relevant Pages

  • Re: Create two-dimensional Array from string
    ... transform into a 2-dimensional array, where each row is an array. ... you're going to use a custom parser instead of the nice DBI ... The problem is a MULTISET returned by DBI/DBD as a varchar and the ...
    (comp.lang.perl.misc)
  • Re: Create two-dimensional Array from string
    ... todo is efficiently ... transform into a 2-dimensional array, where each row is an array. ... the first field for ordering. ...
    (comp.lang.perl.misc)
  • Re: Create two-dimensional Array from string
    ... In this case MULTISET is a collection of rows from a DB. ... transform into a 2-dimensional array, where each row is an array. ... can those single-quoted strings contain ...
    (comp.lang.perl.misc)
  • Re: Create two-dimensional Array from string
    ... todo is efficiently ... transform into a 2-dimensional array, where each row is an array. ... push @records, $record1; ...
    (comp.lang.perl.misc)
  • Re: Create two-dimensional Array from string
    ... pc> array from strings such as ... pc> In this case MULTISET is a collection of rows from a DB. ... pc> transform into a 2-dimensional array, where each row is an array. ... *arrayref DBI functions to get the array back effortlessly. ...
    (comp.lang.perl.misc)