in reply to RE: Re: Simple Text Conversion
in thread Simple Text Conversion
You're just pushing each line arbitrarily onto @recs--you need to group them in sets of 5, because that's what the original file looked like. Plus why are you using foreach in the original reading-in-the-file loop? That
reads the entire file into a temp array--not a horrible thing in many cases, but still, if we can process on a line by line basis, we may as well do so. :)foreach my $line (<FH>)
I like your use of split quite a lot, though.
So, combining your ideas and mine:
Notice I took the array slice out--I think the op wanted everything in the array. If not, though, he/she should stick it back in, justuse strict; open FH, "foo" or die "Can't open: $!"; my @recs; while (<FH>) { chomp; push @{$recs[int(($.-1) / 5)]}, split /,?\s/; } close FH or die "Can't close: $!"; open FH, ">foo.new" or die "Can't open: $!"; print FH map join('|', @$_) . "\n", @recs; close FH;
instead of@$ref[0..4]
And I'm now using map, just cause map is great.@$ref
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Why I Do What I Do
by chromatic (Archbishop) on Apr 06, 2000 at 22:57 UTC |