- 论坛徽章:
- 145
|
本帖最后由 jason680 于 2014-03-11 16:24 编辑
回复 6# pitonas
there is one important things for us:
always to use if conditional to check match command before using $1,$2,...
if(m/..../){
# command with $1, $2, $3
}
input data:
R3_X1_AP7101Dd_D1_M5a02a11_loadpull_v1.txt 557.91277528272
xxxxxxx
S3_Y0_AP7101Dd_D1_M5a02a11_loadpull_v1.txt 595.525554180431
1. using s/../../ will keep the unmatched data
while (<DATA>) {
s/^R(\d+)_X([\d\-]+)_\S+\s+(\S+\s*)$/$1,$2 $3/;
print;
}
output data:
3,1 557.91277528272
xxxxxxx
S3_Y0_AP7101Dd_D1_M5a02a11_loadpull_v1.txt 595.525554180431
2. using m/.../ without if conditional will get some issue
while (<DATA>) {
my ( $A, $B, $C ) = /^\D(\d+)_[^\d\-]+([\d\-]+)_.*\s(\S+\n)/;
print "$A, $B $C";
}
output data:
3, 1 557.91277528272
, 3, 0 595.525554180431
Note: the requester didn't say how to do unmatched data ...
BTW, you can use if(s/../../){ print ...} to drop unmatched data |
|