perl - Parse::FixedLength Trimming Issue -
in earlier question had asked how avoid parse::fixedlength trimming zeros. code @bolav suggested worked sample data using somehow not seem work new data.
it seems should work somehow trimming zeros data. making obvious mistake cannot figure out is. appreciate help.
#!/usr/bin/perl use strict; use warnings; use parse::fixedlength; use data::dumper; $parser = parse::fixedlength->new([ field1 => '12r0:1:12', field2 => '2:13:14', field3 => '5r0:15:19', field4 => '10r0:20:29', field5 => '2r0:30:31', field6 => '3r0:32:34' ], {trim => '1'}); $parser->{tpad}[0] = qr/^0+(?=\d)/; # modification suggested @bolav while (<data>) { warn "no record terminator found!\n" unless chomp; warn "short record!\n" unless $parser->length == length; $data = $parser->parse($_); print dumper $data; } __data__ 119401122910xx42152931177771001000 119401122910xx42152931177771001010 the last field should 0 , 10 outputs blank , 10. update: don't want output field6 000 , 010 -- can removing trim option. regex supposed fix issue reason not doing that.
$var1 = bless( { 'field1' => '119401122910', 'field6' => '', 'field4' => '9311777710', 'field2' => 'xx', 'field3' => '42152', 'field5' => '1' }, 'parse::fixedlength::hashasobj::href1' ); $var1 = bless( { 'field1' => '119401122910', 'field6' => '10', 'field4' => '9311777710', 'field2' => 'xx', 'field3' => '42152', 'field5' => '1' }, 'parse::fixedlength::hashasobj::href1' );
update
okay i've read original question , understand better you're wanting. should make each question stand on own -- stack overflow isn't forum
the reason suggested modification doesn't work configuration $parser->{tpad} array of regular expressions removed front of each justified field. in case it's fields except field2. modifying first element of array, fixing field1
here's more generalised modification changes every element of $parser->{tpad} array leaves @ least final character of field, whatever is. note that, if padding character space in formats 5r trim all-space field down single space instead of emptying it
use strict; use warnings; use parse::fixedlength; use data::dump; $parser = parse::fixedlength->new( [ field1 => '12r0:1:12', field2 => '2:13:14', field3 => '5r0:15:19', field4 => '10r0:20:29', field5 => '2r0:30:31', field6 => '3r0:32:34' ], { trim => 1 } ); $_ = qr/$_(?=.)/ @{ $parser->{tpad} }; while (<data>) { $data = $parser->parse($_); dd $data; } __data__ 119401122910xx42152931177771001000 119401122910xx42152931177771001010 output
bless({ field1 => 119401122910, field2 => "xx", field3 => 42152, field4 => 9311777710, field5 => 1, field6 => 0, }, "parse::fixedlength::hashasobj::href1") bless({ field1 => 119401122910, field2 => "xx", field3 => 42152, field4 => 9311777710, field5 => 1, field6 => 10, }, "parse::fixedlength::hashasobj::href1") i notice code produces result want if remove trim => 1 option. presume have reasons wanting in place here solution
since parse::fixedlength allows pack template elements used, can explicitly specify a field data transferred literally. it's same template module uses other fields, disables trim option field
this code ask
use strict; use warnings; use parse::fixedlength; use data::dump; $parser = parse::fixedlength->new([ field1 => '12r0:1:12', field2 => '2:13:14', field3 => '5r0:15:19', field4 => '10r0:20:29', field5 => 'a2:30:31', field6 => 'a3:32:34' ], {trim => '1'}); while ( <data> ) { $data = $parser->parse($_); dd $data; } __data__ 119401122910xx42152931177771001000 119401122910xx42152931177771001010 output
bless({ field1 => 119401122910, field2 => "xx", field3 => 42152, field4 => 9311777710, field5 => "01", field6 => "000", }, "parse::fixedlength::hashasobj::href1") bless({ field1 => 119401122910, field2 => "xx", field3 => 42152, field4 => 9311777710, field5 => "01", field6 => "010", }, "parse::fixedlength::hashasobj::href1")
Comments
Post a Comment