Ok I'm looking to find the array index of the entered employee number here is my 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
7 2027 -
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
-
}
-
@KevinADC
I get a "syntax error at test2.PL line 90, near "$value;"
Execution of test2.PL aborted due to compilation errors."
Ok just missed a couple things there I've brought it down to this -
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
@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.
@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.
You should be passing a value in here: -
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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];
...
|
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 =...
|
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...
|
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...
|
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.
|
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...
|
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 =...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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...
|
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...
|
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...
| |