in reply to Why doesn't this regex work? (Solved!)
Please understand that I am new to the Monastery. That said, why wouldn't something like this work if the data always has spaces between the numbers?
#!/usr/bin/perl use strict; while( <DATA> ) { my @numbers = split; my $half = int(@numbers/2); my @first = splice(@numbers,0,$half); print "@first\n"; print " @numbers\n"; } __DATA__ 105 106 107 108 109 110 111 112 113 1115 1116 1117 1118 1119 1120 1121 1122 1123 12345 12346 12347 12348 12349 12350 12351 12353
The first snippet just cuts the line in half, but it could easily be adapted to print out lines of X number of values (with continuation rows indented.)
#!/usr/bin/perl use strict; my $maxitems = 3; while( <DATA> ) { my @numbers = split; my $indent = ''; while ( @numbers > 0) { my @rowdata = splice(@numbers,0,$maxitems); print "${indent}@rowdata\n"; $indent = ' '; } } __DATA__ 105 106 107 108 109 110 111 112 113 116 1115 1116 1117 1118 1119 1120 1121 1122 1123 1125 12345 12346 12347 12348 12349 12350 12351 12353 12355
UPDATE: I understand that this doesn't answer the question posed (which is interesting in itself) but I have to wonder if that is the right approach to the task at hand to begin with.
|
|---|