Re: Create two-dimensional Array from string




jjcassidy@xxxxxxxxx writes:

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')}


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

Beware eval with unsanitized user inputs like

http://xkcd.com/327/

--
Joel
.



Relevant Pages