CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Finding data in arrays

 



wyndcrosser
Novice

Oct 7, 2012, 7:43 PM

Post #1 of 10 (3038 views)
Finding data in arrays Can't Post

I have finished all of my assignments for this semester, but this one. I'm way ahead in class, but arrays are not my thing... The following code is what I'm attempting to do. There are no comments yet, as I haven't gotten my code down 100%

There is a menu with three options. 1 is print out the amount of all students owed (sum of @amountDue).

Option 2 is ask for a student ID, if the ID is found print out the name, amount owed, and if the ID isn't found create data in the array with name,amount owed and studentID. Press "0" to back out option 2 menu and return to the main menu (options 1-3).

**option two is where I'm stuck**

Option 3 terminates the program.

I have tried numerous ways such as if statements for if ($temp ~~ @studentID) do blank. This works, but then I can't hit "0" to back out of the program. I have also tried using GREP to validate if data is present in the array. You'll see where $choice == 2 is where I'm having the difficulties at, I have grep there now, I was attempting to debug it. I'm guessing I'm going to need to use a for loop to validate the information in the array and pull from it.

Any support on this, or clarifications for identifying how to look into an array and print out information, would be greatly appreciated.

Also note down where is says "} while ($temp eq 0); "

I initially had until ($temp == 0); I was looking at ways to see why 0 wouldn't bring me back to the main menu.

Code erased.

if someone else if having issues with a similar project you can msg me there and ask.
Perl Newbie - 7 months of PERL basics.

(This post was edited by wyndcrosser on Oct 9, 2012, 6:48 PM)


kola4help
Novice

Oct 7, 2012, 9:09 PM

Post #2 of 10 (3031 views)
Re: [wyndcrosser] Finding data in arrays [In reply to] Can't Post

while ($temp eq 0); means true and hence loop continues

to exit from the loop..change it as........... while ($temp ne 0);


FishMonger
Veteran / Moderator

Oct 7, 2012, 9:16 PM

Post #3 of 10 (3031 views)
Re: [wyndcrosser] Finding data in arrays [In reply to] Can't Post

Since this is for a course homework assignment, I can't/won't give you the code, but I can/will give you some guidance.

There are several ways to approach this problem, but IMO, the best approach would be to use a dispatch table. A dispatch table is a hash where the keys are the menu options and the value of each of them point to a code reference, (i.e., a reference to a subroutine that is executed when that option is entered).

You need 4 subroutines for the dispatch table. One for each menu option and one "default" to handle invalid input.

After prompting/accepting the user's selection, you sanitize it as needed and compare it against the hash keys and execute the appropriate subroutine.

I'm not going to provide the actual code or reference links, because finding that out is part of your task in completing the assignment.

If/when you get stuck, you can post specific questions along with your code and we will help where we can, without doing your homework for you.


(This post was edited by FishMonger on Oct 7, 2012, 9:22 PM)


FishMonger
Veteran / Moderator

Oct 7, 2012, 9:20 PM

Post #4 of 10 (3030 views)
Re: [kola4help] Finding data in arrays [In reply to] Can't Post

Both eq and ne are string comparisons which are the wrong type of comparison to make when comparing against a numerical value.


wyndcrosser
Novice

Oct 7, 2012, 9:42 PM

Post #5 of 10 (3027 views)
Re: [FishMonger] Finding data in arrays [In reply to] Can't Post

until ($temp == 0);

allows me to break the loop and return to menu 1-3.

I'll review over what Fishmonger just told me. thanks.

My hardest issue is verifying how to correctly find the data in studentID and identify what the if statement should do? Does there need to be a loop there?

I'm not asking for "here's some code", but just some clarification, or if someone is willing to guide me through what I'm asking. I don't plan on being a PERL expert, but I would like to figure out what the heck I'm doing :).

This is a basic PERL class, so I'd like to stay in the realm of that thinking as well :).

In Reply To
Perl Newbie - 7 months of PERL basics.

(This post was edited by wyndcrosser on Oct 7, 2012, 9:44 PM)


FishMonger
Veteran / Moderator

Oct 8, 2012, 4:07 AM

Post #6 of 10 (3017 views)
Re: [wyndcrosser] Finding data in arrays [In reply to] Can't Post

Is using arrays a requirement for the assignment? Using separate arrays is a really odd choice of data structure for this task. Instead, you should be using a hash (more specifically, a Hash of Hashes) where the key is the student ID and the value is a hash reference containing the rest of the data.

For example:

Code
my %students = ( 
123 => {
First_Name => 'Bill',
Last_Name => 'Smith',
Amount_Due => 100,
},
321 => {
First_Name => 'John',
Last_Name => 'Doe',
Amount_Due => 50,
},
);


Basic skeleton of script operation:

Code
while (1) { 
# display menu and retrieve user input
# lookup user input in the hash
# execute the subroutine based on hash lookup of user's input
}



(This post was edited by FishMonger on Oct 8, 2012, 4:17 AM)


Laurent_R
Veteran / Moderator

Oct 8, 2012, 4:38 AM

Post #7 of 10 (3012 views)
Re: [wyndcrosser] Finding data in arrays [In reply to] Can't Post


In Reply To

My hardest issue is verifying how to correctly find the data in studentID and identify what the if statement should do?


Not sure what you really want, but maybe you are looking for something like:


Code
if (defined $studentID[$temp]) { # ...


Otherwise, I agree with Fishmonger that a more elaborate data structure would be better than several arrays (could be a HoH or an AoH).


wyndcrosser
Novice

Oct 8, 2012, 5:20 AM

Post #8 of 10 (3008 views)
Re: [Laurent_R] Finding data in arrays [In reply to] Can't Post

Laurent,

That's exactly what I tried before. I don't believe I got any results, as it immediately failed me out of the program.

I'll keep ya updated. Thanks for the support. I'll continue working on it today and tonight.

Wynd
Perl Newbie - 7 months of PERL basics.


Laurent_R
Veteran / Moderator

Oct 8, 2012, 10:34 AM

Post #9 of 10 (2990 views)
Re: [wyndcrosser] Finding data in arrays [In reply to] Can't Post

It should work.

Example under the debugger:



Code
  DB<1> $a[0] = 1; 

DB<2> print "foo" if defined $a[0];
foo
DB<3> print "bar" if defined $a[1];

DB<4>


As you can see, it print "foo" because $a[0] is defined, but does not print "bar" because $a[1] is not defined.

If your try failed, if must be due to something else.


wyndcrosser
Novice

Oct 9, 2012, 6:47 PM

Post #10 of 10 (2960 views)
Re: [Laurent_R] Finding data in arrays [In reply to] Can't Post

I have no clue what you guys are talking about, but this is how i ended up doing it. I'll erase the code from here soon so ppl can't copy it.

Code
#for loop to support looking into the array from the last entry   
for ($i=$#studentID; $i>=0; $i--)
{
#pulls from the for loop looking for the search_key id within the studentID array
if ($studentID[$i] == $search_key)
{
#exits the loop
last;
}
}

#if number is found, display the information for that array number.
if ($i>=0)
{
print "\nStudent ID: \t@studentID[$i]";
print "First Name: \t@firstName[$i]";
print "Last Name: \t@lastName[$i]";
printf ("Amount Due: \t\$%1.2f", @amountDue[$i]);
}

#if not found, print name not found
else
{
print "\nName not found.\n";
}

Perl Newbie - 7 months of PERL basics.

(This post was edited by wyndcrosser on Oct 9, 2012, 7:34 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives