0

I have a college student that works for me and I was trying to help him with his perl project but I couldn't figure out what was going on with it. He's got a txt file of numbers in rows. The numbers have to be sorted on a per row basis and then determine how many triples, pair, or straights ( 3 consecutive numbers) there are on each row. We get the values to show up correctly but at the bottom of the page it doesn't like our ==, !=, or - operators. I was hoping someone might be able to help me understand this a little better so I might be able to explain it to him.

#!/usr/bin/perl
#use strict;
use warnings;
#use diagnostics;

#open file and set file handler
$infile_name = "input.txt";


open(INFILE, $infile_name) or die "Error opening file $!";
#$line = $_;
chomp(@lines = <INFILE>);

#close File handler
close(INFILE) or die "Error - unable to close file $!";
############################################

#Validate sample size
if($lines[0] >= 1 && $lines[0] <= 200000)
{
  $sample_size = $lines[0];
}
else
{
  print "\nInvalid Sample Size!\n";
}

print "\nSample size = ".$sample_size;  #print sample size


#############################################
#print original sample
print "\nHere is the original sample: \n";
foreach $line (@lines)
{
  print "$line";
}
#############################################

#print sorted sample and output whether
#individual is vulnerable or immune to infection
print "\n\nNow sorted: \n";

$susceptibility = "";
$infected = 0;


foreach $line(@lines)
{ 
  @snums = split(' ', $line);
  @nums = sort { $a <=> $b } @snums;
  $size = @nums;
  $pairs = 0;
  $straights = 0;
  $triples = 0;

  #if not equal to 14 characters output vulnerable
  if($size == 1)
  {
    $susceptibility = "";
  }
  elsif($size != 14 && $size != 1)
  {
    $susceptibility = "Immune";
    print "@nums"."\t".$susceptibility."\n";
  }
  elsif($size == 14)
  {
    $i = 0;
    #check for gene combinations
    #while($i <= $#nums)
    for ($i =0; $i < 14;)
    { 
      #Check for triples
      if(($nums[$i] == $nums[$i+1]) && ($nums[$i] == $nums[$i+2]
      ))
      {
        $triples ++;
        $i += 3;
      }


      #Check for pairs
      elsif(($nums[$i] == $nums[$i+1]) && ($nums[$i] != $nums[$i+2])
      && ($nums[$i] != $nums[$i-1]))
      {
        $pairs ++;
        $i += 2;
      }


      #Check for straights
      elsif(($nums[$i] == ($nums[($i+1)]-1)) && ($nums[$i+1] == 
      ($nums[($i+2)]-1)))
      {
        $straights ++;
        $i +=3;
      }

      else
      {
        $i += 1;
      }
    }
     $susceptibility = "Vulnerable";
     $infected ++;

     print "@nums"."\t".$susceptibility."\t"." Triples: $triples".     " Pairs: $pairs"." Straights: $straights"."\n";
  }



}
#############################################

#Print outro
print "\nGene Analysis complete. $infected vulnerable cases found - Eliminate Immediately!\n\n";
#############################################

input.txt file

6
1 3 4 7 9 1 3 4 7 9 1 3 4 7
2 6 2 6 3 5 4 6 2 9 7 8 7 7
9 9 9 6 5 4 3 7 3 8 3 2 2 1
5 5 5 4 3 1 3 2 9 6 9 7 9 8
1 3 2 4 4 7 7 4 5 6 8 9 3
9 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5

We've been compiling this at www.compileonline.com using the PERL option.

2
Contributors
2
Replies
24
Views
3 Years
Discussion Span
Last Post by 2teez
0

After searching google for a little while longer, I found this article. Evidently what he was seeing were some warnings and not actual errors. His code was doing all of the math correctly.

0

Hi MaddTechwf,
Please don't comment out use strict; from your script. It helps a great deal.

Fine, you have what you wanted. But am sure there are several ways of doing what you wanted, even better, I suppose.

There are several codes here one can improve on. But since you have what you wanted nice one bro.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.