473,587 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Undefined Arrays inside for loop

30 New Member
Hello:

Im working on a Hotel Reservtion script and ran into a little problem:
I have 3 types of rooms, 1 is Single, 2 is Double, 3 is Suite
for this reason, $totaltypes = '3'; (in the snippet below).

I have a form with checkboxes in order to reserve rooms, all rooms that are checked are saved in the array @All_Rooms as detailed below:

Expand|Select|Wrap|Line Numbers
  1.  = ("1-101", "1-102","2-201", "2-202","3-301", "3-302");
1-101 means that room number 101 belongs to type 1 (being a single room).
2-201 means that room number 201 belongs to type 2 (being a double room).
and so forth.

Now I need to remove all room numbers from the array @All_Rooms and put them in separate arrays by room type. In other words all rooms beginning with 1- would go in one array, all rooms beginning with 2- would go on another, and so forth.

Except I never know how many room types will be, the script checks out this number by veryfying how many room type files have been created and assign it to the scalar $totaltypes.
I tryed the following code but it doesnt work... what am I doing wrong..??
Expand|Select|Wrap|Line Numbers
  1. for($b=1; $b<=$totaltypes; $b++)
  2. {
  3. foreach $room (@all_rooms){
  4. @splitted_room = split (/\-/, $room);
  5. if($splitted_room[0] == $b){
  6. push(@Reserved_Rooms[$b], $splitted_room[1]);
  7. }
  8. }
  9. }
Thanx beforehand
VirtualWeb
Mar 28 '09 #1
2 2280
KevinADC
4,059 Recognized Expert Specialist
Your best option to me sounds like using a hash of arrays. The type is the hash key and all the room numbers of that type will be the value of the key, which is an array. Something like:

Expand|Select|Wrap|Line Numbers
  1. my %Rooms;
  2. foreach my $room (@all_rooms){
  3.    my ($t, $r) = split (/-/, $room);
  4.    push @{$Rooms{$t}}, $r;
  5. }
  6.  
You then loop through the hash keys and the value of each hash key will have all the rooms associated with the type.

Expand|Select|Wrap|Line Numbers
  1. foreach my $type (keys %Rooms) {
  2.    print "Room Type: $type\n";
  3.    print "Rooms: ", join(' - ', @{$Rooms{$type}} , "\n\n";
  4. }
  5.  
Mar 28 '09 #2
virtualweb
30 New Member
Thank you.. I had never seen a hash of arrays... it worked

Expand|Select|Wrap|Line Numbers
  1. my %Rooms;
  2. foreach my $room (@all_rooms){
  3.    my ($t, $r) = split (/-/, $room);
  4.    push @{$Rooms{$t}}, $r;
  5. }
  6.  
Apr 13 '09 #3

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

Similar topics

2
1959
by: dave.harper | last post by:
I'm relatively new to C++, but have a question regarding functions and arrays. I'm passing a relatively large array to a function several thousand times during the course of a loop, and it seems to get bogged down. Do the arrays previously passed to the function stay memory resident? If not, what's causing it and what can I do to correct...
16
3475
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record in a hidden field) I wish the user to be able to edit the data in the table, so I have extracted the records into hiddenfield, textareas, dropdown...
1
2059
by: ferraro.joseph | last post by:
Hi, I'm querying Salesforce.com via their AJAX toolkit and outputting query results into a table. Currently, their toolkit does not possess the ability to do table joins via their structured query language, which forces me to do the join manually via arrays. Right now, I'm having trouble getting these query results (which are in
12
1796
by: divya | last post by:
1. If The size of the array is unknown during programming time then how is such array declared?? Suppose there is an Array named arrClashname(size unknown at start), Now when the clash occurs I want to store that name into the arrClashname, Therez a do while loop inside which has some conditions which decied when clash occurs and a ...
4
2050
by: mantrid | last post by:
Im using arrays generated from my records displayed in a table on my site to update the corresponding records in a mysql database ie on the web page col1 col2 col3 1 2 2 1 6 2 7 4 which I post to next page as col1array col2array col3array problem is some
1
2437
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are of different lengths (e.g. usually between 25 and 45 rows. The columns in each file have the same length). The text files have been numbered...
5
1990
by: adinda | last post by:
So what i need is this; (I'm very new at this,, programming in C I mean...) In matlab I had a while loop, and after each loop was done I added my resulting matrix to an object. Seeing the loop is conditional to my results I do not know in advance how many loops I need. The matrices are Nx * Ny* Nz*6. In C I put my matrices into 1D...
4
4336
by: =?Utf-8?B?UmljaA==?= | last post by:
I have to read data from an Excel Sheet. Using Microsoft Office Interop and Automation I create an Excel object Dim xl As New Excel.Application, wkbk As Excel.Workbook, rng as Excel.Range I open a workbook and then read the data from a worksheet using the Sheet's UsedRange property into an Excel Range object I call rng. Then I loop...
110
6916
by: fjm | last post by:
For some reason, I have always had a hard time understanding arrays as they pertain to php and databases. I understand associative arrays just fine but when there are multidimensional arrays, I kinda don't. I have gone over a few different examples but they were limited. I was able to find one piece of code that I would like to disect and ask...
0
7915
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.