in reply to splitting line without split
Also, you didn't mention it specifically, but are the two elements from item "(2)" derived only from the second input line? Would the first input line produce two output array elements like this?
If I have that right, then it's true that using "split()" would be a bit clunky, and a regex match would make more sense. For the patterns given, I'd do it like this:$elem[0] = "SHAPES GREEN1;SIZE 240 500 340 930;SIZE 350 500 240 590;SI +ZE 295 390 015 490;SIZE 350 210 760 300;"; $elem[1] = "SHAPES GREEN2;SIZE 450 310 680 690;SIZE 450 110 680 490;SI +ZE 215 800 560 900;";
Or if you want a bunch of matches from various input lines all pushed onto the same array:my @matches = ( $inp_line =~ /(SHAPES \w+;(?:SIZE[ \d+])+;)/g );
Yet another way to look at the problem: if the input lines are being read from some list file (or stdin), such that each line originally has a line-feed at the end, then what you are doing is, in a way, the same as inserting a line-feed in the middle of each line:my @matches; for ( @inp_lines ) { push @matches, ( /(SHAPES \w+;(?:SIZE[ \d+])+;)/g ); }
s/(?<=\d;)(?=SHAPES )/\n/; # using "look-behind" and "look-ahead # now just split on "\n"...
|
|---|