in reply to Convert string to data structure
Another variation on the theme:
#! perl use strict; use warnings; use Data::Dumper; my $str = "1. ...... AB_CD 1.1 ...... EF_GH 1.2 .... IJ_KL_MN 2. ..... +....... OPQR"; $str =~ s/ \.{2,} //gx; $str =~ s/ \.\s //gx; my %terms = split(/\s+/, $str); my @array; push @array, { 'chapter' => $_, 'name' => $terms{$_} } for sort keys % +terms; print Dumper(\@array);
Output:
$VAR1 = [ { 'name' => 'AB_CD', 'chapter' => '1' }, { 'name' => 'EF_GH', 'chapter' => '1.1' }, { 'name' => 'IJ_KL_MN', 'chapter' => '1.2' }, { 'name' => 'OPQR', 'chapter' => '2' } ];
Hope that helps,
Athanasius <°(((>< contra mundum
|
|---|