Printout Problems

Following is how the code stands:

#! /usr/local/bin/perl -w

###prompt for input####
print "Input path and file name:\n";
($a=<STDIN>);

###If cannot open provide error output####
open(IN,$a) || die "Cannot open file $a:$!";

###ensure there is a new line character placed at the end of every ###
###line read in has a \n character at the end###
$/="\n";

####open file for output####
open(OUT,">/home/tmclaugh/z.def")|| die "Cannot open file>:!";
$prev_begin='';
$first_location='';
$other_locations='';
while(<IN>){
        @fields = split(/:/); #array to hold parsed query split by :
        $begin="BEGIN_RESOURCE\nNAME|ZTL$fields[0]$fields[1]\nDATA|$fields[4]|$fields[5]";        
        print OUT $begin if $begin ne $prev_begin;
        $prev_begin=$begin;

        #print the LOCATION lat lng
        $first_location="LOCATION$fields[2]|$fields[3]\n";
        print OUT $first_location;
        $other_locations="LOCATION$fields[2]|$fields[3]\n";
        if ($first_location ne $other_locations){
           print OUT $other_locations;
        }
        print OUT $first_location;
        }
close(IN);
close(OUT);

Following is an example of the input:

0112:0:272800:0775330:0:239
0112:0:272800:0780330:0:239
0112:0:272800:0781900:0:239
0112:0:280000:0790200:0:239
0112:0:300930:0784000:0:239
0112:0:300900:0783400:0:239
0112:0:300640:0780520:0:239
0112:0:293330:0780730:0:239

Following is an example of the current output:


BEGIN_RESOURCE
NAME|ZTL01120
DATA|0|239
LOCATION272800|0775330
LOCATION272800|0775330
LOCATION272800|0780330
LOCATION272800|0780330
LOCATION272800|0781900
LOCATION272800|0781900
LOCATION280000|0790200
LOCATION280000|0790200
LOCATION300930|0784000
LOCATION300930|0784000
LOCATION300900|0783400
LOCATION300900|0783400
LOCATION300640|0780520
LOCATION300640|0780520
LOCATION293330|0780730
LOCATION293330|0780730


This is how the output should look:

BEGIN_RESOURCE
NAME|ZTL01120
DATA|0|239
LOCATION|272800|0775330
LOCATION|272800|0780330
LOCATION|272800|0781900
LOCATION|280000|0790200
LOCATION|300930|0784000
LOCATION|300900|0783400
LOCATION|300640|0780520
LOCATION|293330|0780730
LOCATION|272800|0775330


The idea is to print the first location, print the rest of the locations, and then reprint the first location again. Then repeat the process again throughout the input.

Any help would be appreciated.

Thanks
tomatocansAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

adam923Commented:
#! /usr/local/bin/perl -w
print "Input path and file name:\n";
$a=<STDIN>;
chomp $a;
open(IN,$a) or die "Cannot open file $a:$!";
open(OUT,">/home/tmclaugh/z.def")
      or die "Cannot open file>:!";
while(<IN>){
     @fields = split(/:/); #array to hold parsed query split by :
     $begin="BEGIN_RESOURCE\nNAME|ZTL$fields[0]".
     "$fields[1]\nDATA|$fields[4]|$fields[5]";        
     if($begin ne $prev_begin){
          print OUT $first_location;
          print OUT $begin;  
          $first_location="LOCATION$fields[2]|$fields[3]\n";
          print OUT $first_location;
     }else{
          print "LOCATION$fields[2]|$fields[3]\n";
     }
    $prev_begin=$begin;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tomatocansAuthor Commented:
I don't see this working:


I don't this will work on the input from lines one and two. I think it will print like this:


BEGIN_RESOURCE
NAME|ZTL01120
DATA|0|239
LOCATION|272800|0775330
BEGIN_RESOURCE
NAME|ZTL01120
DATA|0|239
LOCATION|272800|0775330

B/C the first line does not have a previous begin to compare. Do you think this is correct Ozo?


Thanks Adam.
0
adam923Commented:
have you tried the program i provided above? you need to remember that the while loop iterates on one line of input at a time
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

bigbedCommented:
In these lines:
        #print the LOCATION lat lng
        $first_location="LOCATION$fields[2]|$fields[3]\n";
        print OUT $first_location;
        $other_locations="LOCATION$fields[2]|$fields[3]\n";
        if ($first_location ne $other_locations){
           print OUT $other_locations;
        }
        print OUT $first_location;

You *unconditionally* print $first_location before checking whether to print $other_locations, then you *unconditionally* print $first_location again at the end.
This is why you're getting 2 copies of each data set.
Perhaps you need something like:
At the beginning (outside the loop), set $first_location = "";

$other_locations="LOCATION|$fields[2]|$fields[3]\n";
if ($first_location ne $other_locations) {
  print OUT $other_locations;
  $first_location = $other_locations;
}

Note the extra "|" character in the print, too.
0
tomatocansAuthor Commented:
Hey Adam,

Your program works except on the last run through the loop. Everthing on the run ran smooth except this one:

BEGIN_RESOURCE
NAME|ZTL9708M0
DATA|61|100
LOCATION|255100|0821830
LOCATION|261000|0821830
LOCATION|263000|0822600
LOCATION|265525|0823700
LOCATION|270355|0820400
LOCATION|270445|0820000
LOCATION|270700|0815100
LOCATION|265245|0813230
LOCATION|264800|0812800
LOCATION|263900|0812430
LOCATION|263300|0812210
LOCATION|262430|0812200
LOCATION|255100|0812500

The LOCATION|255100|0821830 did not repeat again at the end and this section was the last one.

Would this problem be solved by adding

print OUT $first_location;  after

$prev_begin=$begin;

Thanks
0
tomatocansAuthor Commented:
Hust tried my suggestion no dice.
0
tomatocansAuthor Commented:
Hello Adam this the code that will print the final line. Thanks for your help.

#! /usr/local/bin/perl
print "Input path and file name:\n";
$a=<STDIN>;
chomp $a;
open(IN,$a) || die "Cannot open file $a:$!";
open(OUT,">/home/tmclaugh/l.def") ||die "Cannot open file>:!";
while(<IN>){
           
        @fields = split(/:/); #array to hold parsed query split by :
        $begin="BEGIN_RESOURCE\nNAME|ZTL$fields[0]M$fields[1]\nDATA|$fields[4]|$fields[5]";          
        if($begin ne $prev_begin){
               print OUT $first_location;
               print OUT $begin;  
               $first_location="LOCATION|$fields[2]|$fields[3]\n";
               print OUT $first_location;
        }else{
               print OUT "LOCATION|$fields[2]|$fields[3]\n";
        }
        $prev_begin=$begin;
}
close(IN);
print OUT $first_location;
close(OUT);
0
adam923Commented:
you're absolutely right, i overlooked that last line... sorry
0
tomatocansAuthor Commented:
Hey adam,

upon double checking this code is know printing out the last location for the first and the last sections but not any others. any suggestions on how to correct the problem.


0
adam923Commented:
please post the modified code (if different from what's above) and your entire input file (or a few sections if it's enormous) and i'll run it on my computer.
0
tomatocansAuthor Commented:
Hey adam,

I apologize for the error. after this output runs through a java
program the results are like so with the duplicates from the perl script listed together at the bottom. Is there a way to take this output and one of the last location before a new section to the top.

||ZTL0401M0 101 239
80.57709994265372|998.9766333569121
77.22902970499433|992.410545848063
75.43891064436605|967.5864713186388
35.711102148931445|965.7837066698069
26.161208976093405|970.9158760877679
10.480911494039674|979.436166424292
13.360668332034379|990.3110364651843
27.0590694032835|996.0061192644399
47.95438067797858|1000.8285269999461
54.264873267691655|1002.5292223376837
54.264873267691655|1002.5292223376837
||ZTL0501M1 110 140
46.76767237302249|1058.5198120077023
69.19000433204826|1059.1328560872842
81.49695867456751|1086.5111588716507
97.5633398168722|1068.8332372316293
93.24589002714788|1038.344505527206
101.00628418654128|1019.8080761790343
92.76362923727118|1015.7601647376837
83.59549258159655|1004.8956632297325
80.57709994265372|998.9766333569121
54.264873267691655|1002.5292223376837
47.40478234843175|1015.3460077117384
39.74512491056879|1024.7076496748484
32.525209386642416|1033.9664349440245
38.7312891276427|1048.54654961509
38.7312891276427|1048.54654961509


So this output would be changed to look like so:


||ZTL0401M0 101 239
54.264873267691655|1002.5292223376837
80.57709994265372|998.9766333569121
77.22902970499433|992.410545848063
75.43891064436605|967.5864713186388
35.711102148931445|965.7837066698069
26.161208976093405|970.9158760877679
10.480911494039674|979.436166424292
13.360668332034379|990.3110364651843
27.0590694032835|996.0061192644399
47.95438067797858|1000.8285269999461
54.264873267691655|1002.5292223376837
||ZTL0501M1 110 140
38.7312891276427|1048.54654961509
46.76767237302249|1058.5198120077023
69.19000433204826|1059.1328560872842
81.49695867456751|1086.5111588716507
97.5633398168722|1068.8332372316293
93.24589002714788|1038.344505527206
101.00628418654128|1019.8080761790343
92.76362923727118|1015.7601647376837
83.59549258159655|1004.8956632297325
80.57709994265372|998.9766333569121
54.264873267691655|1002.5292223376837
47.40478234843175|1015.3460077117384
39.74512491056879|1024.7076496748484
32.525209386642416|1033.9664349440245
38.7312891276427|1048.54654961509

Thanks for your input.

0
adam923Commented:
it looks very doable, and it's in fact what i intended my original modification to do, but as i said in my comment before i need your input file if i'm going to run this on my comptuer
0
tomatocansAuthor Commented:
The last example is the final input file. I just want a final script to take the output from java, shown in last comment:

||ZTL0401M0 101 239
80.57709994265372|998.9766333569121
77.22902970499433|992.410545848063
75.43891064436605|967.5864713186388
35.711102148931445|965.7837066698069
26.161208976093405|970.9158760877679
10.480911494039674|979.436166424292
13.360668332034379|990.3110364651843
27.0590694032835|996.0061192644399
47.95438067797858|1000.8285269999461
54.264873267691655|1002.5292223376837
54.264873267691655|1002.5292223376837
||ZTL0501M1 110 140
46.76767237302249|1058.5198120077023
69.19000433204826|1059.1328560872842
81.49695867456751|1086.5111588716507
97.5633398168722|1068.8332372316293
93.24589002714788|1038.344505527206
101.00628418654128|1019.8080761790343
92.76362923727118|1015.7601647376837
83.59549258159655|1004.8956632297325
80.57709994265372|998.9766333569121
54.264873267691655|1002.5292223376837
47.40478234843175|1015.3460077117384
39.74512491056879|1024.7076496748484
32.525209386642416|1033.9664349440245
38.7312891276427|1048.54654961509
38.7312891276427|1048.54654961509


Move the last location to the top so the scipt will produce output that looks like so:

||ZTL0401M0 101 239
54.264873267691655|1002.5292223376837
80.57709994265372|998.9766333569121
77.22902970499433|992.410545848063
75.43891064436605|967.5864713186388
35.711102148931445|965.7837066698069
26.161208976093405|970.9158760877679
10.480911494039674|979.436166424292
13.360668332034379|990.3110364651843
27.0590694032835|996.0061192644399
47.95438067797858|1000.8285269999461
54.264873267691655|1002.5292223376837
||ZTL0501M1 110 140
38.7312891276427|1048.54654961509
46.76767237302249|1058.5198120077023
69.19000433204826|1059.1328560872842
81.49695867456751|1086.5111588716507
97.5633398168722|1068.8332372316293
93.24589002714788|1038.344505527206
101.00628418654128|1019.8080761790343
92.76362923727118|1015.7601647376837
83.59549258159655|1004.8956632297325
80.57709994265372|998.9766333569121
54.264873267691655|1002.5292223376837
47.40478234843175|1015.3460077117384
39.74512491056879|1024.7076496748484
32.525209386642416|1033.9664349440245
38.7312891276427|1048.54654961509


The output will be placed into excel so a graph can be produced.
0
adam923Commented:
1. since when does java have anything to do with this?
2. you're showing some output in those last two comments, did you just make it up or is it from a perl script?  If it is from perl, what was the input.  If not I still need a sample input file from you with multiple "sections"
0
tomatocansAuthor Commented:
Thanks Adam. I appreciate your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.