?
Solved

Loop That Pa

Posted on 2000-05-10
13
Medium Priority
?
164 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
13 Comments
 

Author Comment

by:tomatocans
ID: 2797973
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
ID: 2797991
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 200 total points
ID: 2798020
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

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

Author Comment

by:tomatocans
ID: 2798037
The output should return the what position in the string the anomaly appears.


0
 

Author Comment

by:tomatocans
ID: 2798051
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
 
LVL 12

Expert Comment

by:geotiger
ID: 2798107
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
ID: 2798130
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
ID: 2800493
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
ID: 2800528
The answer is not it will not be contained twice
in the substring
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2800531
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
ID: 2802546
Adjusted points from 38 to 50
0
 

Author Comment

by:tomatocans
ID: 2802547
Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

764 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