Looking for a value in an array and returning the index

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chanshaw
    New Member
    • Nov 2008
    • 67

    Looking for a value in an array and returning the index

    Ok I'm looking to find the array index of the entered employee number here is my code

    Code:
    #!/usr/bin/perl
    # Uses module Text::CSV
    # compiler directives
    use strict;
    use warnings;
    use Text::CSV;
    
    my $grossPay;
    my $empNum;
    my $numHours;
    my (@empsNums, @empsName, @empsROP, @empsDept);
    my $fileName = "emp.csv";
    my @data;
    my $status;
    my @fields;
    my $csv = Text::CSV->new();
    my $counter = 0;
    my $recordNum
    
    &loadFile;
    &getUserInput;
    &search;
    &displayCalc;
    
    sub getUserInput
    {
        print "Please enter your employee number: ";
        $empNum = <>;
        chomp($empNum);
        print "Please enter your number of hours worked: ";
        $numHours = <>;
        chomp($numHours);
    }
    
    sub loadFile
    {
        open (MYFILE, $fileName);
        while (<MYFILE>)
        {
            chomp;
            $data[$counter] = $_;
               my $status = $csv->parse ($data[$counter]);
            @fields = $csv->fields();
            my($i) = 0;
            foreach my $field (@fields)
               {
                if($i == 0)
                {
                    $empsNums[$counter] = $field;
                }
                if($i == 1)
                {
                    $empsName[$counter] = $field;
                }
                if($i == 2)
                {
                    $empsDept[$counter] = $field;
                }
                if($i == 3)
                {
                    $empsROP[$counter] = $field;
                }
                $i++;
               }
            $i = 0;
            $counter++;
        }
        close (MYFILE);
    }
    
    sub displayCalc
    {
        print "\n";
        print "        Gross Pay Calculation";
        print "    \nEmployee Number: ".$empsName[$empNum];
        print "    \nEmployee Name: ".$empsName[$empNum];
        print " \nEmployee Rate of Pay: ".$empsROP[$empNum];
        print " \nEmployee Hours Worked: ".$numHours;
        print " \nEmployee Gross Pay: ".$grossPay;
        print " \n\n";
    }
    
    sub queryEmpNum
    {
    }
    
    sub calcGPay
    {
        $grossPay = $numHours * $empsROP[$empNum];
    }
    The queryEmpNum should set the $recordNum
    So like if I enter 105 it should look in the @empsNums array for the number 105 and then return the array index number of that value. thanks
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Code:
    sub queryEmpNum {
       my $value = $_[0];
       for my $i (0..$#empsNums) {
          return $i if ($empsNums[$i] == $value;
       }
       return -1;# would indicate to the caller it found nothing
    }

    Comment

    • chanshaw
      New Member
      • Nov 2008
      • 67

      #3
      Originally posted by KevinADC
      Code:
      sub queryEmpNum {
         my $value = $_[0];
         for my $i (0..$#empsNums) {
            return $i if ($empsNums[$i] == $value;
         }
         return -1;# would indicate to the caller it found nothing
      }
      I get a "syntax error at test2.PL line 90, near "$value;"
      Execution of test2.PL aborted due to compilation errors."

      Comment

      • chanshaw
        New Member
        • Nov 2008
        • 67

        #4
        Ok just missed a couple things there I've brought it down to this

        Code:
        sub queryEmpNum 
        {
            my $value = $_[0];
            for my $i (0..$#empsNums)
            {
                return $i if ($empsNums[$i] == $empNum);
                }
            return -1;
        }
        I cant seem to get it to return the index of the array that contains $empNum. so like if $empNum is in the array store the index number in $indexNum

        Comment

        • eWish
          Recognized Expert Contributor
          • Jul 2007
          • 973

          #5
          What is it returning?

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by chanshaw
            Ok just missed a couple things there I've brought it down to this

            Code:
            sub queryEmpNum 
            {
                my $value = $_[0];
                for my $i (0..$#empsNums)
                {
                    return $i if ($empsNums[$i] == $empNum);
                    }
                return -1;
            }
            I cant seem to get it to return the index of the array that contains $empNum. so like if $empNum is in the array store the index number in $indexNum

            Sorry about the syntax error. How are you calling the queryEmpNum funciton? Are you passing in an argument? Where the argument is the value you are looking for in the array.

            Comment

            • chanshaw
              New Member
              • Nov 2008
              • 67

              #7
              Originally posted by KevinADC
              Sorry about the syntax error. How are you calling the queryEmpNum funciton? Are you passing in an argument? Where the argument is the value you are looking for in the array.
              Hey thanks for the help Kevin, I was able to figure out a solution. Here's the code for the solution if it helps anyone.

              Code:
              #!/usr/bin/perl
              # Uses module Text::CSV
              # compiler directives
              use strict;
              use warnings;
              use Text::CSV;
              
              #by: Colin Hanshaw
              my $grossPay;
              my $empNum;
              my $numHours;
              my (@empsNums, @empsName, @empsROP, @empsDept);
              my $fileName = "emp.csv";
              my @data;
              my $status;
              my @fields;
              my $csv = Text::CSV->new();
              my $counter = 0;
              my $arrayIndex;
              my $running = 1;
              
              &loadFile;
              
              while($running == 1)
              {
                  &getUserInput;
                  &findEmp;
                  &calcGPay;
                  &displayCalc;
              }
              
              sub getUserInput
              {
                  print "Please enter your employee number (or enter 0 to quit): ";
                  $empNum = <>;
                  chomp($empNum);
                  if($empNum == 0)
                  {
                      exit;
                  }
                  print "Please enter your number of hours worked: ";
                  $numHours = <>;
                  chomp($numHours);
              }
              
              sub loadFile
              {
                  open (MYFILE, $fileName);
                  while (<MYFILE>)
                  {
                      chomp;
                      $data[$counter] = $_;
                         my $status = $csv->parse ($data[$counter]);
                      @fields = $csv->fields();
                      my($i) = 0;
                      foreach my $field (@fields)
                         {
                          if($i == 0)
                          {
                              $empsNums[$counter] = $field;
                          }
                          if($i == 1)
                          {
                              $empsName[$counter] = $field;
                          }
                          if($i == 2)
                          {
                              $empsDept[$counter] = $field;
                          }
                          if($i == 3)
                          {
                              $empsROP[$counter] = $field;
                          }
                          $i++;
                         }
                      $i = 0;
                      $counter++;
                  }
                  close (MYFILE);
              }
              
              sub displayCalc
              {
                  print "\n";
                  print "        Gross Pay Calculation\n";
                  print "    \nEmployee Number:       ".$empsNums[$arrayIndex];
                  print "    \nEmployee Name:         ".$empsName[$arrayIndex];
                  print " \nEmployee Department:   ".$empsDept[$arrayIndex];
                  print " \nEmployee Rate of Pay:  ".$empsROP[$arrayIndex];
                  print " \nEmployee Hours Worked: ".$numHours;
                  print " \nEmployee Gross Pay:    ".$grossPay;
                  print " \n\n";
              }
              
              sub findEmp
              {
                  $arrayIndex = &queryEmpNum;
              }
              
              sub queryEmpNum 
              {
                  my $value = $_[0];
                  for my $i (0..$#empsNums)
                  {
                      return $i if ($empsNums[$i] == $empNum);
                      }
                  return -1;
              }
              
              sub calcGPay
              {
                  $grossPay = $numHours * $empsROP[$arrayIndex];
              }

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                You should be passing a value in here:

                Code:
                sub findEmp
                {
                    $arrayIndex = &queryEmpNum($arg);
                }
                Where $arg is the value you want to find in the array and get the index number of. It also kind of silly to have a function that all it does is call another function.

                Comment

                Working...