Solved

Loop That Pa

Posted on 2000-05-10
13
147 Views
Last Modified: 2010-03-05
Have input for $fields[11] like so:
ALT DEV/EXCURSION FROM ASSIGNED;ACFT EQUIPMENT PROBLEM/CRITICAL

Have the following loop reading input:


while(<IN>){

           @anomalies=split(/;/,$fields[11]);      
      $number=0;
      $i=1;
      #LIST OF ANOMALIES
      $1="NO";   #ACFT EQUIPMENT PROBLEM/CRITICAL
      $2="NO";   #ACFT EQUIPMENT PROBLEM/LESS SEVERE
      $3="NO";   #ALT DEV/EXCURSION FROM ASSIGNED
      $4="NO";   #ALT DEV/OVERSHOOT ON CLB OR DES
      for($i;$i< 4;$i++){
            $number++;
            if(@anomalies[i]="ACFT EQUIPMENT PROBLEM/CRITICAL"){
                  $1="YES";
                  @count[$number];
            }
            elsif(@anomalies[i]="ACFT EQUIPMENT PROBLEM/CRITICAL"){
                  $2="YES";
                  @count[$number];
            }
            elseif(@anomalies[i]="ACFT EQUIPMENT PROBLEM/LESS SEVERE"){
                  $3="YES";
                  @count[$number];
            }
            else(@anomalies[i]="ALT DEV/EXCURSION FROM ASSIGNED"){
                  $4="YES";
                  $count++;
            }
      }


      print OUT "$1|$count[$number]|$2|$count[$number]|$3|$count[$number]|$4|$count[$number]";

}


The line of input in the example should print

NO|0|YES|2|NO|0|YES|1


How would u correct the code to properly produce the output.



0
Comment
Question by:tomatocans
  • 8
  • 4
13 Comments
 

Author Comment

by:tomatocans
Comment Utility
Have input for $fields[11] like so:
ALT DEV/EXCURSION FROM ASSIGNED;ACFT EQUIPMENT PROBLEM/CRITICAL

Have the following loop reading input:


while(<IN>){

           @anomalies=split(/;/,$fields[11]);      
      $number=0;
      $i=1;
      #LIST OF ANOMALIES
      $1="NO";   #ACFT EQUIPMENT PROBLEM/CRITICAL
      $2="NO";   #ACFT EQUIPMENT PROBLEM/LESS SEVERE
      $3="NO";   #ALT DEV/EXCURSION FROM ASSIGNED
      $4="NO";   #ALT DEV/OVERSHOOT ON CLB OR DES
      for($i;$i< 4;$i++){
            $number++;
            if(@anomalies[i]="ACFT EQUIPMENT PROBLEM/CRITICAL"){
                  $1="YES";
                  @count[$number];
            }
            elsif(@anomalies[i]="ACFT EQUIPMENT PROBLEM/CRITICAL"){
                  $2="YES";
                  @count[$number];
            }
            elseif(@anomalies[i]="ACFT EQUIPMENT PROBLEM/LESS SEVERE"){
                  $3="YES";
                  @count[$number];
            }
            else(@anomalies[i]="ALT DEV/EXCURSION FROM ASSIGNED"){
                  $4="YES";
                  @count[$number];
            }
      }


      print OUT "$1|$count[$number]|$2|$count[$number]|$3|$count[$number]|$4|$count[$number]";

}


The line of input in the example should print

NO|0|YES|2|NO|0|YES|1


How would u correct the code to properly produce the output.



0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
given that
$field[11] contains ALT DEV/EXCURSION FROM ASSIGNED;ACFT EQUIPMENT PROBLEM/CRITICAL

& also give that

$1 looks for ACFT EQUIPMENT PROBLEM/CRITICAL
$2 looks for ACFT EQUIPMENT PROBLEM/LESS SEVERE
$3 looks for ALT DEV/EXCURSION FROM ASSIGNED
$4 looks for  ALT DEV/OVERSHOOT ON CLB OR DES

shouldn't the output be....

NO|1|YES|0|NO|1|YES|0

??

i am assuming that the numbers are the count of times that each sub-string has been found in the mail string ($field[11]).
0
 
LVL 16

Accepted Solution

by:
maneshr earned 50 total points
Comment Utility
based on my above assumption, here is a working solution...

NOTE: you cannot assign values yourself to $1,$2 etc.. since these are PERL read-only variables.

================tcans.pl
#!/usr/local/bin/perl

$fields[11]="ALT DEV/EXCURSION FROM ASSIGNED;ACFT EQUIPMENT PROBLEM/CRITICAL";

@anomalies=split(/\;/,$fields[11]);
$number=0;
$i=1;
#LIST OF ANOMALIES
$one="NO";   #ACFT EQUIPMENT PROBLEM/CRITICAL
$two="NO";   #ACFT EQUIPMENT PROBLEM/LESS SEVERE
$three="NO";   #ALT DEV/EXCURSION FROM ASSIGNED
$four="NO";   #ALT DEV/OVERSHOOT ON CLB OR DES

$count{'one'}=0;
$count{'two'}=0;
$count{'three'}=0;
$count{'four'}=0;
foreach(@anomalies){
  if ($_ eq "ACFT EQUIPMENT PROBLEM/CRITICAL"){
    $one="YES";
    $count{'one'}++;
  }elsif ($_ eq "ACFT EQUIPMENT PROBLEM/LESS SEVERE"){
    $two="YES";
    $count{'two'}++;
  }elsif ($_ eq "ALT DEV/EXCURSION FROM ASSIGNED"){
    $three="YES";
    $count{'three'}++;
  }elsif ($_ eq "ALT DEV/OVERSHOOT ON CLB OR DES"){
    $four="YES";
    $count{'four'}++;
  }
}

print "$one|$count{'one'}|$two|$count{'two'}|$three|$count{'three'}|$four|$count{'four'}\n";
0
 

Author Comment

by:tomatocans
Comment Utility
No it is the postion in the string not the count.
I apologize.
0
 

Author Comment

by:tomatocans
Comment Utility
The output should return the what position in the string the anomaly appears.


0
 

Author Comment

by:tomatocans
Comment Utility
In the example

ALT DEV/EXCURSION FROM ASSIGNED;ACFT EQUIPMENT PROBLEM/CRITICAL


ALT DEV/EXCURSION FROM ASSIGNED is first
ACFT EQUIPMENT PROBLEM/CRITICAL is second

so the ouput should be

YES|2|NO|0|YES|1|NO|0

Sorry about the confusion

0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 12

Expert Comment

by:geotiger
Comment Utility
If I understand your question correctly, this may be what you are looking for:


#!/usr/local/bin/perl

$lst  = "ACFT EQUIPMENT PROBLEM/CRITICAL";
$lst .= ":ACFT EQUIPMENT PROBLEM/CRITICAL";
$lst .= ":ACFT EQUIPMENT PROBLEM/LESS SEVERE";
$lst .= ":ALT DEV/EXCURSION FROM ASSIGNED";

@a = split /:/, $lst;
$c[0] = "ALT DEV/EXCURSION FROM ASSIGNED;ACFT EQUIPMENT PROBLEM/CRITICAL";
$c[1] = "ALT DEV/EXCURSION FROM ASSIGNED;ACFT EQUIPMENT PROBLEM/CRITICAL";
$c[1] .= "; ;ALT DEV/OVERSHOOT ON CLB OR DES";
$c[3] = " ; ; ;ALT DEV/EXCURSION FROM ASSIGNED";
$rst = "";

%b = ();
# while(<IN>){
for $j (0..$#c) {
    # @anomalies=split(/;/,$fields[11]);
    @anomalies=split(/;/,$c[$j]);
    for $i (0..$#anomalies) {
        if ("$anomalies[$i]" eq "$a[$i]") { ++$b{'YES'}[$i];
        } else { ++$b{'NO'}[$i]; }  
    }
}
# }
for $i (0..3) {
    if ($b{'YES'}[$i] > 0) {
        if ($rst) { $rst .= "|YES|" . $b{'YES'}[$i];
        } else    { $rst  = "YES|" . $b{'YES'}[$i]; }
    } else {
        if ($rst) { $rst .= "|NO|0"; } else { $rst = "NO|0"; }
    }
}
# print OUT "$rst\n";
print "$rst\n";

# The line of input in the example should print
# NO|0|YES|2|NO|0|YES|1


The output is
NO|0|YES|2|NO|0|YES|1

0
 

Author Comment

by:tomatocans
Comment Utility
Sorry this is too hard coded the input
i have has 66,000 lines with

30 types of anomalies.

I agree this is a solution for the
specific example but
I am look for something like mansher
was doing but with position instead
of count.

In your example geotiger I would
need to much hard coding for the $c[N]
area.

Is there a proper solution within the framework
that Maneshr was exploring. Just substituting position for count.
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
tomatocans,

i have a Q. can the string contain the same substring twice ??

Eg.
ALT DEV/EXCURSION FROM ASSIGNED;ACFT EQUIPMENT PROBLEM/CRITICAL;ALT DEV/EXCURSION FROM ASSIGNED

if yes, what should the result be in this case since the sub-string ALT DEV/EXCURSION FROM ASSIGNED appears at position 1 and 3!!

if the answer is no, i have a solution ready for you.
0
 

Author Comment

by:tomatocans
Comment Utility
The answer is not it will not be contained twice
in the substring
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
here is your solution....

i have had to make some very small changes to the original script to make it work.

========tcans.pl
#!/usr/local/bin/perl

$fields[11]="ALT DEV/EXCURSION FROM ASSIGNED;ACFT EQUIPMENT PROBLEM/CRITICAL";
@anomalies=split(/\;/,$fields[11]);
$number=0;
$i=1;
#LIST OF ANOMALIES
$one="NO";   #ACFT EQUIPMENT PROBLEM/CRITICAL
$two="NO";   #ACFT EQUIPMENT PROBLEM/LESS SEVERE
$three="NO";   #ALT DEV/EXCURSION FROM ASSIGNED
$four="NO";   #ALT DEV/OVERSHOOT ON CLB OR DES

$count{'one'}=0;
$count{'two'}=0;
$count{'three'}=0;
$count{'four'}=0;
#foreach(@anomalies){
foreach($[ .. $#anomalies){
  if ($anomalies[$_] eq "ACFT EQUIPMENT PROBLEM/CRITICAL"){
    $one="YES";
    $count{'one'}=($_+1);
  }elsif ($anomalies[$_] eq "ACFT EQUIPMENT PROBLEM/LESS SEVERE"){
    $two="YES";
    $count{'two'}=($_+1);
  }elsif ($anomalies[$_] eq "ALT DEV/EXCURSION FROM ASSIGNED"){
    $three="YES";
    $count{'three'}=($_+1);
  }elsif ($anomalies[$_] eq "ALT DEV/OVERSHOOT ON CLB OR DES"){
    $four="YES";
    $count{'four'}=($_+1);
  }
}

print "$one|$count{'one'}|$two|$count{'two'}|$three|$count{'three'}|$four|$count{'four'}\n";

0
 

Author Comment

by:tomatocans
Comment Utility
Adjusted points from 38 to 50
0
 

Author Comment

by:tomatocans
Comment Utility
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now