Hi Everyone,

I would like to get suggestions on how to efficiently write a program. I explain below what the program does.

I have an array with values say 1 to 70. And I have a list which has values 1,10,20,40,60. Basically I need to output the array into different files where the first file will have values from 1 to 9, second from 10 to 19, third from 20 to 39, fourth from 40 to 59 and fifth from 60 to end.

I am assuming the best way to write this is to go through the array only once and not multiple times. But, I need to open 5 files simultaneously and first write a 1 line header into all of them and then start printing out the elements of the array.

Please let me know your suggestions on how to do this right or if the problem is not clear.

Thanks,

Jagan

Here's what I have written. Works perfectly and gives me what I need but I want to know if it is the best way to write it or can I write it better. Please let me know if you don't understand things in the code. The foreach loop goes through the array, opens a new file, write the 1 line header, then checks if the element needs to be written to the file and if not then closes the current file and a new file is opened in the next iteration.
#!/usr/bin/perl -w use strict; use warnings; my $num_args=$#ARGV+1; if($num_args<2) { print "Usage: doc_create.pl <particle folder> <particle list>" +; exit; } chomp(my $part_folder=$ARGV[0]); chomp(my $part_list=$ARGV[1]); open PLIST,"<$part_list" || die "Can't open file: $!"; my @part_id=<PLIST>; close PLIST; foreach (@part_id) { chomp($_); } #print "@part_id"; my $part_id=join(" ",@part_id); print "$part_id\n"; my @part_list=split(" ",$part_id); #foreach (@part_list) { # print "$_\n"; #} my $pattern=$part_folder."_???.spi"; my $particles=`ls $part_folder/$pattern`; #print "$particles\n"; my @particles=split(/\n/,$particles); my $doublet=1; my $i=0; open OFDOC,">images.doc" || die "Can't open file: $!"; open OFSEL,">images.sel" || die "Can't open file: $!"; print OFDOC " ; Headerinfo columns: rot (1), tilt (2), psi (3), Xoff ( +4), Yoff (5), Zoff (6), Ref (7), Wedge (8), Pmax/sumP (9), LL (10)\n" +; foreach (@particles) { chomp($_); my $part_id=substr($_,-7,-4); #print "$part_id\n"; if ($i==0) { open ODOC,">images_$doublet.doc" || die "Can't open fi +le: $!"; open OSEL,">images_$doublet.sel" || die "Can't open fi +le: $!"; print ODOC " ; Headerinfo columns: rot (1), tilt (2), +psi (3), Xoff (4), Yoff (5), Zoff (6), Ref (7), Wedge (8), Pmax/sumP +(9), LL (10)\n"; $i++; goto CHECK; } else { open ODOC,">>images_$doublet.doc" || die "Can't open f +ile: $!"; open OSEL,">>images_$doublet.sel" || die "Can't open f +ile: $!"; CHECK: my $x=2*$doublet-1; my $y=2*$doublet+1; if ($doublet<9) { my $start_id=$part_list[$x]; my $end_id=$part_list[$y]; if ($part_id>=$start_id && $part_id<$end_id) { print OSEL "../$_ $doublet\n"; print ODOC " ; ../$_\n"; printf ODOC " %3d 10 0.00000 0. +00000 0.00000 0.00000 0.00000 0.00000 %.5f 1.00000 + 0.00000 0.00000\n",$part_id,$doublet; print OFSEL "../$_ $doublet\n"; print OFDOC " ; ../$_\n"; printf OFDOC " %3d 10 0.00000 0 +.00000 0.00000 0.00000 0.00000 0.00000 %.5f 1.00000 + 0.00000 0.00000\n",$part_id,$doublet; } else { $doublet++; $i=0; close ODOC; close OSEL; } } else { my $start_id=$part_list[$x]; if ($part_id>=$start_id) { print OSEL "../$_ $doublet\n"; print ODOC " ; ../$_\n"; printf ODOC " %3d 10 0.00000 0. +00000 0.00000 0.00000 0.00000 0.00000 %.5f 1.00000 + 0.00000 0.00000\n",$part_id,$doublet; print OFSEL "../$_ $doublet\n"; print OFDOC " ; ../$_\n"; printf OFDOC " %3d 10 0.00000 0 +.00000 0.00000 0.00000 0.00000 0.00000 %.5f 1.00000 + 0.00000 0.00000\n",$part_id,$doublet; } else { $doublet++; $i=0; close ODOC; close OSEL; } } } } close OFDOC; close OFSEL;

In reply to Suggestion on a Program by ojagan

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.