Re: Create two-dimensional Array from string
- From: jjcassidy@xxxxxxxxx
- Date: Thu, 3 Apr 2008 08:49:11 -0700 (PDT)
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 ];
}
.
- Follow-Ups:
- Re: Create two-dimensional Array from string
- From: pjfalbe@xxxxxxxxx
- Re: Create two-dimensional Array from string
- From: jgraber
- Re: Create two-dimensional Array from string
- References:
- Create two-dimensional Array from string
- From: pjfalbe@xxxxxxxxx
- Create two-dimensional Array from string
- Prev by Date: New errors of export image format
- Next by Date: Win32::OLE "Member not found"
- Previous by thread: Re: Create two-dimensional Array from string
- Next by thread: Re: Create two-dimensional Array from string
- Index(es):
Relevant Pages
|