Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Splitting file into separate files based on record lengths and identifiers

by Generoso (Parson)
on Aug 25, 2010 at 22:55 UTC ( #857332=note: print w/replies, xml ) Need Help??


in reply to Splitting file into separate files based on record lengths and identifiers

Hope this will help you. Uses Hash.

#!/usr/bin/perl -w # use warnings; use strict; my %hash = (); while (<DATA>) { chomp($_); my $n = length($_); my $i = 0; while ($i<$n) { my $long = substr($_,$i,4); $i += 4; my $delim = substr($_,$i,1); $i += 1; my $val = substr($_,$i,$long); $i += $long; # print $long,' ',$delim, ' ', $val,"\n"; $hash{ $delim } .= $val.','; } } s/,\z// for values %hash; while ( my ($key, $value) = each(%hash) ) { print "$key => $value\n"; } print "size of hash: " . keys( %hash ) . ".\n"; print '-' x (60),"\n"; __DATA__ 0004$ADAM0002*330004%19770004$BOB 0002*430004%1967 0003$XDA0002*440004%22220003$XOB0002*990004%3333

Results.

Process started >>> $ => ADAM,BOB ,XDA,XOB % => 1977,1967,2222,3333 * => 33,43,44,99 size of hash: 3. ------------------------------------------------------------ <<< Process finished.

Replies are listed 'Best First'.
Re^2: Splitting file into separate files based on record lengths and identifiers
by monty77 (Initiate) on Aug 26, 2010 at 21:53 UTC

    The hash solution intrigued me, but it seems to break when I make the strings longer, can someone enlighten me as to why? Sample data:

    0100$THIS IS A 100 CHAR FIELD $%%$                                                               0030*THIS IS A 30 CHAR ONE    

    Thanks!

      It is working fin for me. The sign comes out funny because I an in Latinamerica and using the standard character set.

      DATA

      0004$ADAM0002*330004%19770004$BOB 0002*430004%1967 0003$XDA0002*440004%22220003$XOB0002*990004%3333 0100$THIS IS A 100 CHAR FIELD $%%$ + 12345 0030*THIS IS A 30 CHAR ONE

      Result

      Process started >>> $ => ADAM,BOB ,XDA,XOB,THIS IS A 100 CHAR FIELD $%%$ + 12345 % => 1977,1967,2222,3333 * => 33,43,44,99,THIS IS A 30 CHAR ONE size of hash: 3. ------------------------------------------------------------ <<< Process finished.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://857332]
help
Chatterbox?
[Corion]: ambrus: Yeah, I read that, but it's somewhat vague as in what I really have to implement, and where/how my "other" mainloop should/needs to call AnyEvent
[Corion]: (or maybe I just work better from existing code that I munge until it works and I understand it rather than a short abstract text like "implement everything that's needed" ;) )
[ambrus]: Corion: I think in this case you can get away with only a stub for idle, one that always dies when you create it, because AnyEvent::HTTP doesn't use it, not even indirectly through AnyEvent::Handle or AnyEvent::Socket or AnyEvent::DNS.
[Corion]: The "and I understand it" part is optional.
[Corion]: ambrus: Yes but I also need to implement the file / IO watcher, because Prima has that (in Prima::File), and I need to supply the appropriate thing to make push_write etc. work with Prima
[ambrus]: Corion: yes, you need to implement the io watcher, which should be simple because Prima::File is basically that, and the timer watcher form Prima::Timer
[Corion]: ... or so I think. As I said, I'm somewhat vague on how to make AnyEvent cooperate with a callback-driven IO event loop that gives me callbacks when data is available or can be written

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2016-12-08 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (141 votes). Check out past polls.