Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: end of line help

by oko1 (Deacon)
on Nov 18, 2010 at 02:18 UTC ( #872112=note: print w/ replies, xml ) Need Help??

in reply to end of line help

> {...} not sure what os original files were on.

In that case, I would strongly suggest not treating these files as text - which is what you're doing with 'while(<INFILE>)' (which reads from the filehandle one "line" at a time - the definition of 'line' being determined by the value of $/.)

You've mentioned that these files are binary. To me, that suggests that they have some kind of structure. If so, you'd probably be better off either parsing them and modifying only the part that you want (assuming you know the data structure) or slurping the entire file as a string - assuming that they're a relatively small proportion of your memory size - and modifying the string. Something like this, maybe:

#!/usr/bin/perl -w use strict; open my $Link, "linkfile.txt" or die "linkfile.txt: $!\n"; while (<$Link>){ next if $. == 1; # Skip the first line chomp; my ($infile, $labno, $out, $anonno) = split /\t/; print "Anonymizing $infile\n"; open my $In, $infile or die "$infile: $!\n"; binmode $In; my $content = do { local $/; <$In> }; close $In; $content =~ s?B27-\d{1,3}?B27-$anonno?gsm; open my $Out, ">", $out or die "$out: $!\n"; binmode $Out; print $Out $content; close $Out; }

"Language shapes the way we think, and determines what we can think about."
-- B. L. Whorf

Comment on Re: end of line help
Download Code
Replies are listed 'Best First'.
Re^2: end of line help
by mjscott2702 (Pilgrim) on Nov 18, 2010 at 09:29 UTC
    Definitely agree with the idea of slurping the whole file into a string in binary mode, assuming it fits - or maybe there is a variant of Tie::File for binary files that might help here (I did a quick search on CPAN and nothing jumped out)?

    In any case, if you are transferring files between systems, and possibly different OS's using something like FTP - make sure you use binary mode for the transfer.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2015-11-27 12:14 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (727 votes), past polls