Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: raw data formatting

by Kenosis (Priest)
on Nov 16, 2012 at 01:06 UTC ( #1004096=note: print w/ replies, xml ) Need Help??


in reply to raw data formatting

Perhaps the following, which uses List::MoreUtils's natatime to grab five elements at a time from a list of file lines, will be helpful:

use strict; use warnings; use List::MoreUtils qw/natatime/; my $it = natatime 5, <DATA>; while ( chomp( my @lines = $it->() ) ) { my $letter = 'A'; my $acctNum = do { $lines[0] =~ /\s+(\d+)\s+(\d+)/; $1 . $2 }; push @lines, " acctnum=$acctNum"; print for map { s/\s+/$letter++ . ' '/e; "$_\n" } @lines; } __DATA__ Place your data here...

Update: Added a chomp; removed the non-destructive substitution modifier (/r) and a print "\n"; line.


Comment on Re: raw data formatting
Select or Download Code
Re^2: raw data formatting
by teamassociated (Novice) on Nov 16, 2012 at 01:45 UTC
    Looks awesome, but I get errors: it does not like the /r, but does fine with /e except the output is all 11111.
    # perl -c foobar Bareword found where operator expected at foobar line 12, near "s/\s+/ +$letter++ . ' '/er" syntax error at foobar line 12, near "s/\s+/$letter++ . ' '/er " foobar had compilation errors. root@facs04ap [/var/adm/scripts] # perl -v This is perl, v5.8.8 built for aix-thread-multi

    code from u

    use strict; use warnings; use List::MoreUtils qw/natatime/; my $it = natatime 5, <DATA>; while ( my @lines = $it->() ) { my $letter = 'A'; my $acctNum = do { $lines[0] =~ /\s+(\d+)\s+(\d+)/; $1 . $2 }; push @lines, " acctnum=$acctNum"; print for map { s/\s+/$letter++ . ' '/er } @lines; print "\n"; } __DATA__ 50###### 8 Z Axxx ,xxxxxxx 19793.43 02/02/2012 06/08 +/2012 32989.05 037###### S JOYCE xxxxxxS .00 06/06/xxxx + 0 0 7 1471 xxxx xxxx x #101 .00 02/08/2012 + 06/15/2012 NOV 0 U MILWAUKEE, WI xxxxx 32989.05 02/08/2012 + 3 3##### MILWAUKEE WI .00 5321 +4 12 50##### 8 Z Axxx ,Nxxxxx 75.00 05/27/2012 06/ +05/2012 1845.00 03######3 E Nxxxx Axxxx .00 09/xx/xxxx 06/ +05/2012 MYT##### 7####### S 156xxx xxxxx BLVD .00 06/06/2012 06/ +14/2012 MYT######## NOV 0 C TAYLOR,MI 48180 1845.00 05/27/2012 + 3 3 37###### S TAYLOR MI .00 48 +180 3 12 50##### 9 Z ALxxxx Rxxxx ,SUSxxx 75.00 05/05/2012 05/15 +/2012 2045.00 0000000000 E SUSE Axxxxx Rxxxx .00 03/xx/xxxx 05/1 +5/2012 AJT9####### 31####### 44xxx xxx AVE .00 05/16/2012 0 +6/15/2012 AJT92###### NOV 0 C LINCOLN PARK,MI xxxxx 2045.00 05/05/2012 + 1 3 0 S LINCOLN PARK MI .00 4 +8146 1 12

      Your version of Perl doesn't support the non-destructive substitution modifier (/r). Use the following:

      print for map { s/\s+/$letter++ . ' '/e; $_ } @lines;

      Will make that change in the original posting.

        Thank you that worked! This code technique is new 2 me, but thanks! I need to skip certain lines ...working in original code, but tried using it here. how do i skip indexes as opposed to lines?
        while ( my @lines = $it->() ) { next if ($. == 1..88); my $letter = 'A'; my $acctNum = do { $lines[0] =~ /\s+(\d+)\s+(\d+)/; $1 . $2 }; push @lines, " acctnum=$acctNum"; print for map { s/\s+/$letter++ . ' '/e; $_ } @lines; print "\n"; }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2014-08-22 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (164 votes), past polls