473,396 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
7 2027
KevinADC
4,059 Expert 2GB
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
@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
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
971 Expert 512MB
What is it returning?
Feb 3 '09 #5
KevinADC
4,059 Expert 2GB
@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
@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
4,059 Expert 2GB
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

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

Similar topics

2
by: Mountain Man | last post by:
Hi, I'm having trouble with the foreach function. I'm using it twice inside a user defined function with two different arrays, and in the second instance it's returning the value of the first...
5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
2
by: deko | last post by:
Can I return the index number of an array if all I have is the element? For example, if I want to index the alphabet, I can put the letters in the array: Dim varLtr As Variant varLtr =...
7
by: fdunne2 | last post by:
Hi, Is there a function in C that returns the max value in an array? I need a function that returns the max value and the corresponding array index. Also, when reading float values from a...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
13
by: Jordan Tiona | last post by:
Is this not allowed in C++? I try in MSVC, but I always get an error, and the function no longer shows up in my class view.
5
by: Peter Nofelt | last post by:
Hi all, My scenario is as follows: * Receive a base 1 array (index starts at 1 instead of 0) of data from a COM component . * Need to pass this array to a .net component that requires its...
4
by: SM | last post by:
Hello, Knowing the index position of an array, how can i get the next $key available, if it exists. I've found a solution but I was wondering if there was a shorcut in PHP. i.e: $cats =...
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.