Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Convert string to data structure

by johngg (Abbot)
on Sep 21, 2012 at 16:52 UTC ( #994945=note: print w/ replies, xml ) Need Help??


in reply to Convert string to data structure

A variation using splits and maps.

$ perl -MData::Dumper -Mstrict -Mwarnings -E ' > my $str = > q{1. ...... AB_CD 1.1 ...... EF_GH 1.2 .... IJ_KL_MN 2. ......... +... OPQR}; > my $raChapters = [ > map { { chapter => $_->[ 0 ], name => $_->[ 1 ] } } > map { [ split m{[. ]{2,}} ] } > split m{ (?=\d)}, $str > ]; > print Data::Dumper->Dumpxs( [ $raChapters ], [ qw{ raChapters } ] ); +' $raChapters = [ { 'name' => 'AB_CD', 'chapter' => '1' }, { 'name' => 'EF_GH', 'chapter' => '1.1' }, { 'name' => 'IJ_KL_MN', 'chapter' => '1.2' }, { 'name' => 'OPQR', 'chapter' => '2' } ]; $

Cheers,

JohnGG


Comment on Re: Convert string to data structure
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://994945]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2014-11-26 07:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (164 votes), past polls