Re: Create two-dimensional Array from string



On Apr 3, 2:05 pm, jgra...@xxxxxx wrote:
jjcass...@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

Oh yeah. But you have to admit: it's snazzy. :)
.



Relevant Pages

  • Re: K&R2 Secition 5.9 - major blunders
    ... Each element of b doesn't point to a 20 element array of int. ... This mistake is crucial because ... my explanation is really the qualities of something else: ... > The use of the array of pointers is to store the strings. ...
    (comp.lang.c)
  • Re: K&R2 Secition 5.9 - major blunders
    ... Each element of b doesn't point to a 20 element array of int. ... This mistake is crucial because ... my explanation is really the qualities of something else: ... > The use of the array of pointers is to store the strings. ...
    (comp.lang.c)
  • Re: Returning array of strings through callback from unmanaged to
    ... Is the array of strings a jagged array or fixed length array? ... It is imperative that I am able to return the array of strings from the ... I have control over how many strings I want the unmanaged code to return ... What I want to accomplish is to pass a callback function to unmanaged ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: attempting to return values from array from w/in a function
    ... What I get is an array w/ only the most recent array entry. ... I am new to PHP & do not have formal scripting ... you end up comparing strings, but the test you mean probably is simply: ... case 1: {codeblock} ...
    (comp.lang.php)
  • Re: K&R2 Secition 5.9 - major blunders
    ... Would changing 'point to a' to 'point into a' twenty element array be ... > arrays of pointers is to store character strings of diverse ... comparison between what was really happening (arrays of pointers to ... pointer to a string(this probably would confuse beginners)" and ...
    (comp.lang.c)