Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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 drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2015-07-04 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls