ojagan has asked for the wisdom of the Perl Monks concerning the following question:
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Suggestion on a Program
by choroba (Cardinal) on Dec 02, 2015 at 16:34 UTC | |
by ojagan (Novice) on Dec 02, 2015 at 19:06 UTC | |
|
Re: Suggestion on a Program
by 1nickt (Canon) on Dec 02, 2015 at 16:32 UTC | |
|
Re: Suggestion on a Program
by AnomalousMonk (Archbishop) on Dec 02, 2015 at 21:14 UTC |