Solved

Position in String and YES/NO (Sorry will add points tomorrow)

Posted on 2000-05-10
8
200 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]);
                      $position=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";
                      @position[$number];
                      }
                      elsif(@anomalies[i]="ACFT EQUIPMENT PROBLEM/CRITICAL"){
                      $2="YES";
                      @position[$number];
                      }
                      elseif(@anomalies[i]="ACFT EQUIPMENT PROBLEM/LESS SEVERE"){
                      $3="YES";
                      @position[$number];
                      }
                      else(@anomalies[i]="ALT DEV/EXCURSION FROM ASSIGNED"){
                      $4="YES";
                      @position[$number];
                      }
                      }


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

                      }


                      The line of input in the example should print

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


Yes means the anomaly is present in the string and
the



                      How would u correct the code to properly produce the output.
0
Comment
Question by:tomatocans
  • 4
  • 2
  • 2
8 Comments
 

Author Comment

by:tomatocans
ID: 2798149
Also there are 66,000 or so lines of input
for $field[11] and 30 anomalies so the position
of the aomalies can't be hardcoded.

I will ADD 200 points tomorrow
0
 
LVL 12

Expert Comment

by:geotiger
ID: 2798188
The following codes should satisties your new requirement. Do you have some sample data?

#!/usr/local/bin/perl

# just add all your anomalies (30?) here separated by ":"
$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 = ();   # will contains counts
# $i - record the positions of anomalies
# while(<IN>){
for $j (0..$#c) {
    # @anomalies=split(/;/,$fields[11]);
    @anomalies=split(/;/,$c[$j]);
    for $i (0..$#anomalies) {    # check each field
        for $k (0..$#a) {        # against a list of anomalies
            if ("$anomalies[$i]" eq "$a[$k]") { ++$b[$i]; }
        }
    }
}
# }
for $i (0..$#b) {
    if ($b[$i] > 0) {
        if ($rst) { $rst .= "|YES|" . $b[$i];
        } else    { $rst  = "YES|" . $b[$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
0
 
LVL 12

Expert Comment

by:geotiger
ID: 2798232
I do not know what position you want: anomalies in the input line or anomalies in the list such as in @a?

If it is the position in anomaly list, just change the subscript in $b[] from $i to $k in the first for loop.

I have done a lot of similiar data processing. Post some data sample, I will get more sense what you are doing.

 
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2800498
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:tomatocans
ID: 2800530
The answer is no it is not in the substring twice.
0
 
LVL 16

Accepted Solution

by:
maneshr earned 25 total points
ID: 2800534
here is your solution.

========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: 2802549
Adjusted points from 0 to 25
0
 

Author Comment

by:tomatocans
ID: 2802550
Thanks
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

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 …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

705 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

19 Experts available now in Live!

Get 1:1 Help Now