Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Extracting specific data from fixed-width columns

by jds17 (Pilgrim)
on Jul 03, 2008 at 18:44 UTC ( #695435=note: print w/replies, xml ) Need Help??

in reply to Extracting specific data from fixed-width columns

From your description, I am not 100% sure about how your input data is structured, I assume all variables can appear in any one of the three key columns. A working solution would be as follows: (Sorry if it looks strange, I don't know how your data looks and have used your column widths of 25 and 15. I have just printed out the matches, of course you can e.g. save them in a hash or call a function immediately on them, depending on what you want to do with the data.)
use strict; use warnings; #define a regexp matching the interesting variable names my $interesting_vars = qr(a111111111111111111111111|c222222222222222222222222); #sample input rows my @rows = ('a111111111111111111111111 1b8888888888888888 +888888888 15x222222222222222222222222 2', 'd999999999999999999999999 4b3333333333333333 +333333333 15c222222222222222222222222 123'); for (@rows) { #split by variable value pairs for (/.{40}/g) { #split variable and value /(.{25})(.{15})/; #since I am doing an additional match, I have to #save my submatches my $var = $1; my $val = $2; print "'$var' = '$val'\n" if $var =~ $interesting_vars; } }
'a111111111111111111111111' = ' 1' 'c222222222222222222222222' = ' 123'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://695435]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2018-05-24 00:47 GMT
Find Nodes?
    Voting Booth?