in reply to Foreach/While - Why does this run AFTER that?

so you are wanting to split up the file in chunks and compare against one another. sounds very familiar with some stuff i do. why not just split up the file into chunks manually and check it?
open $hddfile, '<whateverfile'; binmode it; my $length = -s $hddfile; my $chunks = $length/ #divided by how ever many chunks you want my $num = #make this to the amount you divided the file into my $counter = 0; my $name = whatever; foreach (1 .. $num){ open $temp, '>', "$name$counter"; #should split up file based on +whatever size you want read $hddfile, my $buf, $chunks; #someone correct me it im wrong + haha print $temp, $buf; $counter++; } open $cmpfile, '<file to compare it with'; binmode it; my $length = -s $cmpfile; my $chunks = $length/ #divided by how ever many chunks you want my $num = #match to the division number my $counter = 0; my $name = whatever; foreach (1 .. $num){ open $temp, '>', "$name$counter"; #should split up file based on +whatever size you want read $hddfile, my $buf, $chunks; #someone correct me it im wrong + haha print $temp, $buf; $counter++ }


then compare them with an sha or md5 checksum tool :) tho this might not be working code, but to me it is a little easier to understand. if it were me i would split the files up into seperate folders in a shared root directory. then compare them.

Replies are listed 'Best First'.
Re^2: Foreach/While - Why does this run AFTER that?
by AnomalousMonk (Archbishop) on Oct 06, 2014 at 13:35 UTC

    Just a few thoughts on your pseudo-code:

    my $chunks = $length/ #divided by how ever many chunks you want

    This would seem to give you a chunk length rather than a number of chunks, but you are subsequently iterating over  $chunks as if it were a number of chunks.

    foreach (0 .. $chunks){
        ...
    }

    This gives an off-by-one error if  $chunks is the number of chunks over which you want to iterate. Either
        foreach (0 .. $chunks - 1) { ... }
    or
        foreach (1 .. $chunks) { ... }
    would seem to do a better job (if knowing the chunk index is not an issue).

      yeah thought about that last night and was just able to get back abd change it :) i added a $num, match it to the amount you divided by. so if you divided the file into 4... then foreach (1 .. 4){.
      br>
Re^2: Foreach/While - Why does this run AFTER that?
by CalebH (Acolyte) on Oct 06, 2014 at 12:42 UTC

    This approach would work, but it seems like it does double the work. For example, this would create several small files on a drive, which would then have to be deleted.

    Let's say as an example that this was run on 20+ .torrent files, all equaling 500mb. In the end, without a deletion, I would have around 9GB stored on the drive. Also, even if the data was deleted as the script ran, it would be taking up 9GB of space in the Recycle Bin (at least I think; I'm not sure if perl deletes to the recycle bin or just removes it entirely.) FWIW, I'm running perl on Win7 64bit.

    I do like your idea, though :)