Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Re: Re: Regex to match 20 chars of some digits followed by some spaces

by tachyon (Chancellor)
on Dec 19, 2003 at 03:35 UTC ( [id://315737]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Regex to match 20 chars of some digits followed by some spaces
in thread Regex to match 20 chars of some digits followed by some spaces

Ah that makes more sense. If you have to deal with fixed width records you may find this sub handy:

$str = 'first name EOFlast name EOFaddress field + EOF'; my @rec_def = ( [ 'first_name', 20 ], [ 'last_name', 20 ], [ 'address', 30 ], ); sub parse_fixed_width { my ( $record, $rec_def ) = @_; my %struct; my $offset = 0; for my $rec(@$rec_def) { $struct{$rec->[0]} = substr $record, $offset, $rec->[1]; $offset += $rec->[1]; } return length($record) == $offset ? \%struct : ''; } use Data::Dumper; print Dumper parse_fixed_width( $str, \@rec_def ); __DATA__ $VAR1 = { 'first_name' => 'first name EOF', 'address' => 'address field EOF', 'last_name' => 'last name EOF' };

cheers

tachyon

  • Comment on Re: Re: Re: Regex to match 20 chars of some digits followed by some spaces
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://315737]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2025-01-25 06:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (70 votes). Check out past polls.