Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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; } }
Output:
'a111111111111111111111111' = ' 1' 'c222222222222222222222222' = ' 123'


Comment on Re: Extracting specific data from fixed-width columns
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2015-07-05 07:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls