chinamox has asked for the wisdom of the Perl Monks concerning the following question:
Hello all,
I am working on finishing up a class project involving splitting up a large file into several smaller ones using IO::File. I have nearly completed writing the script but now when I try to run it on my *INX machine I get no Error messages or feedback, but am not returned to the command line either. When I sign back on to the machine, I find that only one new file has been written in the directory. I think there for that this is likely something wrong in my second while() statement.
I have been working on this problem for a few hours and haven't gotten anywhere. I think this likely results from a very stupid newbie mistake, and thus I am asking for your help. As always, many thanks to any of you who spare me some time.
#!/opt/bin/perl -w use strict; use IO::File; use File::Basename qw(basename); my $input = ""; #used as counters my $fn_count = $ARGV[0] - ($ARGV[0] - 1); my $fn_cyc_count= $ARGV[0]; my $org_fn; #used for File::Basename my $base; my $path; my $type; main(); exit(0); sub main{ while($fn_cyc_count >= 1){ open(IN, $ARGV[1])||die "Cannot open $ARGV[1]:$!\n"; die "No number of lines\n" unless($ARGV[0] =~ /^\d+$/); #Get $max_ln_count per file using $ARGV[0] $lines = 0; $lines ++ while(<IN>); close IN; my $max_ln_count = int($lines/$ARGV[0]); #Get Input from $ARGV[1] open(INFILE, '<' ,$ARGV[1]) ||die "Cannot open $ARGV[1] + for data input!\n"; my $fh = IO::File->new($ARGV[1], 'r') ||die "unable to open $ +ARGV[1] for reading.\n"; my @all_lines = $fh->getlines(); my @rev_all_lines = reverse (@all_lines); #Output one of the new files in the current directory while($max_ln_count >= 1){ #Use File::Basename to get the file name. $org_fn = $ARGV[1]; my $base = basename $org_fn; my $file = $base . $fn_count; #Cut and Write Output my $line = pop(@rev_all_lines); open (OUTFILE, '>' ,$file)||die "Cannot open $file to + write output!\n"; print OUTFILE "$line" . "\n"; #Subtract one from the $max_ln_count counter $max_ln_count = $max_ln_count - 1; #Close method when counter reaches 0 close (OUTFILE) if ($max_ln_count <= 0); } #Subtract one from the $fn_cyc_count counter $fn_cyc_count = $fn_cyc_count - 1; #Close method when counter reaches 0 close (INFILE) if ($fn_cyc_count <= 0); } }
Also there is one other piece of advice I would ask of more experienced Perl coder. As you can see from the code, I am determining the number of lines to be written in each new file by using the / operator. My problem is what to do if there is a remainder? Is there a way to scoop up the remainder as and save it as a $scalar? Any advice on using that to add an extra line to each file until $remainder == 0?
Thank you again for any and all help,
-moxSorry all the command line looks somthing like this
username $: perl myprog.pl 4 /path/path/path/flie.ext ##$ARGV[0] is the number of output files desired.
May God bless you monks, one and all!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem with multiple loops
by davorg (Chancellor) on Oct 24, 2006 at 12:18 UTC | |
by chinamox (Scribe) on Oct 24, 2006 at 12:57 UTC | |
|
Re: Problem with multiple loops
by liverpole (Monsignor) on Oct 24, 2006 at 12:26 UTC | |
by chinamox (Scribe) on Oct 24, 2006 at 13:29 UTC | |
|
Re: Problem with multiple loops
by Melly (Chaplain) on Oct 24, 2006 at 12:48 UTC | |
by chinamox (Scribe) on Oct 24, 2006 at 13:22 UTC | |
by Melly (Chaplain) on Oct 24, 2006 at 13:47 UTC | |
|
Re: Problem with multiple loops
by Samy_rio (Vicar) on Oct 24, 2006 at 12:30 UTC | |
by chinamox (Scribe) on Oct 24, 2006 at 13:26 UTC | |
by Melly (Chaplain) on Oct 24, 2006 at 13:56 UTC | |
by chinamox (Scribe) on Oct 24, 2006 at 14:08 UTC | |
by davorg (Chancellor) on Oct 24, 2006 at 14:00 UTC | |
|
Re: Problem with multiple loops
by graff (Chancellor) on Oct 25, 2006 at 06:35 UTC |