use strict; use warnings; use Data::Dumper; my @shapeLines = ( q{SHAPES GREEN1;SIZE 240 500 340 930;SIZE 350 500 240 590;SIZE 295 390 015 490;SIZE 350 210 760 300;SHAPES GREEN2;SIZE 450 310 680 690;SIZE 450 110 680 490;SIZE 215 800 560 900;}, q{SHAPES GREEN;SIZE 240 500 340 930;SIZE 350 500 240 590;SIZE 295 390 015 490;SIZE 350 210 760 300;SHAPES BLUE;SIZE 450 310 680 690;SIZE 450 110 680 490;SIZE 215 800 560 900;}, ); my @shapes = (); foreach my $shapeLine ( @shapeLines ) { my @posns = (); my $posn = -1; while ( ( $posn = index( $shapeLine, q{SHAPES}, $posn ) ) > -1 ) { push @posns, $posn; $posn ++; } foreach my $posnIdx ( 0 .. $#posns ) { push @shapes, substr $shapeLine, $posns[ $posnIdx ], $posnIdx == $#posns ? length( $shapeLine ) - $posns[ $posnIdx ] : $posns[ $posnIdx + 1 ] - $posns[ $posnIdx ]; } } print Data::Dumper->Dumpxs( [ \ @shapes ], [ qw{ *shapes } ] ) #### $ ./spw805781 @shapes = ( 'SHAPES GREEN1;SIZE 240 500 340 930;SIZE 350 500 240 590;SIZE 295 390 015 490;SIZE 350 210 760 300;', 'SHAPES GREEN2;SIZE 450 310 680 690;SIZE 450 110 680 490;SIZE 215 800 560 900;', 'SHAPES GREEN;SIZE 240 500 340 930;SIZE 350 500 240 590;SIZE 295 390 015 490;SIZE 350 210 760 300;', 'SHAPES BLUE;SIZE 450 310 680 690;SIZE 450 110 680 490;SIZE 215 800 560 900;' ); $