By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,852 Members | 2,198 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,852 IT Pros & Developers. It's quick & easy.

Looking for a value in an array and returning the index

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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. # Uses module Text::CSV
  3. # compiler directives
  4. use strict;
  5. use warnings;
  6. use Text::CSV;
  7.  
  8. my $grossPay;
  9. my $empNum;
  10. my $numHours;
  11. my (@empsNums, @empsName, @empsROP, @empsDept);
  12. my $fileName = "emp.csv";
  13. my @data;
  14. my $status;
  15. my @fields;
  16. my $csv = Text::CSV->new();
  17. my $counter = 0;
  18. my $recordNum
  19.  
  20. &loadFile;
  21. &getUserInput;
  22. &search;
  23. &displayCalc;
  24.  
  25. sub getUserInput
  26. {
  27.     print "Please enter your employee number: ";
  28.     $empNum = <>;
  29.     chomp($empNum);
  30.     print "Please enter your number of hours worked: ";
  31.     $numHours = <>;
  32.     chomp($numHours);
  33. }
  34.  
  35. sub loadFile
  36. {
  37.     open (MYFILE, $fileName);
  38.     while (<MYFILE>)
  39.     {
  40.         chomp;
  41.         $data[$counter] = $_;
  42.            my $status = $csv->parse ($data[$counter]);
  43.         @fields = $csv->fields();
  44.         my($i) = 0;
  45.         foreach my $field (@fields)
  46.            {
  47.             if($i == 0)
  48.             {
  49.                 $empsNums[$counter] = $field;
  50.             }
  51.             if($i == 1)
  52.             {
  53.                 $empsName[$counter] = $field;
  54.             }
  55.             if($i == 2)
  56.             {
  57.                 $empsDept[$counter] = $field;
  58.             }
  59.             if($i == 3)
  60.             {
  61.                 $empsROP[$counter] = $field;
  62.             }
  63.             $i++;
  64.            }
  65.         $i = 0;
  66.         $counter++;
  67.     }
  68.     close (MYFILE);
  69. }
  70.  
  71. sub displayCalc
  72. {
  73.     print "\n";
  74.     print "        Gross Pay Calculation";
  75.     print "    \nEmployee Number: ".$empsName[$empNum];
  76.     print "    \nEmployee Name: ".$empsName[$empNum];
  77.     print " \nEmployee Rate of Pay: ".$empsROP[$empNum];
  78.     print " \nEmployee Hours Worked: ".$numHours;
  79.     print " \nEmployee Gross Pay: ".$grossPay;
  80.     print " \n\n";
  81. }
  82.  
  83. sub queryEmpNum
  84. {
  85. }
  86.  
  87. sub calcGPay
  88. {
  89.     $grossPay = $numHours * $empsROP[$empNum];
  90. }
  91.  
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
Feb 3 '09 #1
Share this Question
Share on Google+
7 Replies


KevinADC
Expert 2.5K+
P: 4,059
Expand|Select|Wrap|Line Numbers
  1. sub queryEmpNum {
  2.    my $value = $_[0];
  3.    for my $i (0..$#empsNums) {
  4.       return $i if ($empsNums[$i] == $value;
  5.    }
  6.    return -1;# would indicate to the caller it found nothing
  7. }  
  8.  
Feb 3 '09 #2

P: 67
@KevinADC
I get a "syntax error at test2.PL line 90, near "$value;"
Execution of test2.PL aborted due to compilation errors."
Feb 3 '09 #3

P: 67
Ok just missed a couple things there I've brought it down to this

Expand|Select|Wrap|Line Numbers
  1. sub queryEmpNum 
  2. {
  3.     my $value = $_[0];
  4.     for my $i (0..$#empsNums)
  5.     {
  6.         return $i if ($empsNums[$i] == $empNum);
  7.         }
  8.     return -1;
  9. }
  10.  
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
Feb 3 '09 #4

eWish
Expert 100+
P: 971
What is it returning?
Feb 3 '09 #5

KevinADC
Expert 2.5K+
P: 4,059
@chanshaw

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.
Feb 3 '09 #6

P: 67
@KevinADC
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.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. # Uses module Text::CSV
  3. # compiler directives
  4. use strict;
  5. use warnings;
  6. use Text::CSV;
  7.  
  8. #by: Colin Hanshaw
  9. my $grossPay;
  10. my $empNum;
  11. my $numHours;
  12. my (@empsNums, @empsName, @empsROP, @empsDept);
  13. my $fileName = "emp.csv";
  14. my @data;
  15. my $status;
  16. my @fields;
  17. my $csv = Text::CSV->new();
  18. my $counter = 0;
  19. my $arrayIndex;
  20. my $running = 1;
  21.  
  22. &loadFile;
  23.  
  24. while($running == 1)
  25. {
  26.     &getUserInput;
  27.     &findEmp;
  28.     &calcGPay;
  29.     &displayCalc;
  30. }
  31.  
  32. sub getUserInput
  33. {
  34.     print "Please enter your employee number (or enter 0 to quit): ";
  35.     $empNum = <>;
  36.     chomp($empNum);
  37.     if($empNum == 0)
  38.     {
  39.         exit;
  40.     }
  41.     print "Please enter your number of hours worked: ";
  42.     $numHours = <>;
  43.     chomp($numHours);
  44. }
  45.  
  46. sub loadFile
  47. {
  48.     open (MYFILE, $fileName);
  49.     while (<MYFILE>)
  50.     {
  51.         chomp;
  52.         $data[$counter] = $_;
  53.            my $status = $csv->parse ($data[$counter]);
  54.         @fields = $csv->fields();
  55.         my($i) = 0;
  56.         foreach my $field (@fields)
  57.            {
  58.             if($i == 0)
  59.             {
  60.                 $empsNums[$counter] = $field;
  61.             }
  62.             if($i == 1)
  63.             {
  64.                 $empsName[$counter] = $field;
  65.             }
  66.             if($i == 2)
  67.             {
  68.                 $empsDept[$counter] = $field;
  69.             }
  70.             if($i == 3)
  71.             {
  72.                 $empsROP[$counter] = $field;
  73.             }
  74.             $i++;
  75.            }
  76.         $i = 0;
  77.         $counter++;
  78.     }
  79.     close (MYFILE);
  80. }
  81.  
  82. sub displayCalc
  83. {
  84.     print "\n";
  85.     print "        Gross Pay Calculation\n";
  86.     print "    \nEmployee Number:       ".$empsNums[$arrayIndex];
  87.     print "    \nEmployee Name:         ".$empsName[$arrayIndex];
  88.     print " \nEmployee Department:   ".$empsDept[$arrayIndex];
  89.     print " \nEmployee Rate of Pay:  ".$empsROP[$arrayIndex];
  90.     print " \nEmployee Hours Worked: ".$numHours;
  91.     print " \nEmployee Gross Pay:    ".$grossPay;
  92.     print " \n\n";
  93. }
  94.  
  95. sub findEmp
  96. {
  97.     $arrayIndex = &queryEmpNum;
  98. }
  99.  
  100. sub queryEmpNum 
  101. {
  102.     my $value = $_[0];
  103.     for my $i (0..$#empsNums)
  104.     {
  105.         return $i if ($empsNums[$i] == $empNum);
  106.         }
  107.     return -1;
  108. }
  109.  
  110. sub calcGPay
  111. {
  112.     $grossPay = $numHours * $empsROP[$arrayIndex];
  113. }
  114.  
Feb 4 '09 #7

KevinADC
Expert 2.5K+
P: 4,059
You should be passing a value in here:

Expand|Select|Wrap|Line Numbers
  1. sub findEmp
  2. {
  3.     $arrayIndex = &queryEmpNum($arg);
  4. }
  5.  
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.
Feb 4 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.