Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: korta - split large files

by graff (Chancellor)
on Sep 20, 2005 at 16:56 UTC ( [id://493523]=note: print w/replies, xml ) Need Help??


in reply to korta - split large files

In this little chunk of your code:
# nonsense to know the file size if($ARGV[0]eq"-info"){ $len=length($file); print $len."\n"; exit 1; }
you are simply reporting the number of characters in the file name that the user has given on the command line. In order to state the actual byte count of data in the file, you want:
$len = -s $file; # not length($file)
I would recommend that since you are already using "my" declarations a lot, you should go ahead and include "use strict" as well. It might help catch some problems like:

$size is first mentioned in the "read()" call for the "-partir" operation; this means that you are passing an undefined value to "read()" for its "LENGTH" parameter, which means you don't read anything. Maybe you meant to put something like "$MAX_SIZE_PART*1024" there instead?

You never know what mistakes users will make on command line args, so I would advise more careful sanity checks on the @ARGV values. Consider using one of the Getopt modules. At a minimum, this:

if(($ARGV[2]=~m/-t/)==1){ $MAX_SIZE_PART=substr($ARGV[2],2); }
should be:
if ( $ARGV[2] =~ /^-t(\d+)/ ) { $MAX_SIZE_PART = $1; } else { die "Usage: blah blah\n"; }
Also, it looks like you are reading the entire content of the file into memory. Since this is for handling really big files, it would be better to read and write a chunk at a time, and not save each successive chunk by pushing them all onto an array.

Then there's also something very confusing about your use of $nro_partes (which probably should be spelled the same as the one declared at the top as "my $nro_parte;") -- you increment this by one on each read, but you compare it to $MAX_SIZE_PART.

Assuming that you have been using some version of this code that actually works, I believe that you have posted some different version, because I doubt that the code as originally posted will work.

Replies are listed 'Best First'.
Re^2: korta - split large files
by Anonymous Monk on Sep 21, 2005 at 10:52 UTC
    Thank you very much for correcting me... As I explain in my comments, I'm not really a programmer, and, well, I'm begining with perl. You are right! I have downloaded the script and found a difference with the one I use... I don't declare "my $size=1024". If you do this, the script will work. I think I posted the code before correcting the bug :-( With $no_partes, what the script do is read parts of $MAX_SIZE_PART. When $no_partes is less than $MAX_SIZE_PART it gets out with the rest of the file. Ej: 120 mgs into 50, 50 and 20. Thank you for your advice about @ARGV... I'm not very clear about the use of the arguments by now. I have to learn a lot yet ;-) And it's true, I should better read and write a chunk at a time... In fact, my hard disk seems like if it's going to explode each time I run the script. I know that the first thing to do when you want to program in Perl is look at the CPAN, but I know that the best way for learn programing is make programs... And I was sooo proud about this script! Well, I'll try to be more careful with the code before post it. Thank you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-16 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found