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,

-mox

Update

Sorry 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!


In reply to Problem with multiple loops by chinamox

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.