• Aucun résultat trouvé

Tracking Customers with a Queue

Dans le document This book is dedicated to its readers. (Page 195-200)

A queue is a container that is analogous to a group of people standing in line at a movie theater.

The theater receptionists sell tickets to people on the basis of their positions in the line. The first person in the line receives a ticket first. When you arrive, you line up last in line.

An array can work like a queue if you control it using the push() and shift() functions.

As Figure 7.16 illustrates, the push() function inserts a new element into the array in an index position greater than those of existing members. The shift() position extracts the element of the array that occupies the lowest index position.

Figure 7.16 Thepush() and shift() functions turn an array into a queue.

Like stacks, queues receive use throughout the programming world as accumulators. Consider a situation in which a large group of people show up at a bank and want to receive help filling out forms. The banking associates who provide this service must prepare background material so that they can serve those whom they counsel. Further, they wish to have the customers who arrive distributed fairly evenly among the available service representatives. Also, the principle stands that the first person to arrive should be the first person served. Figure 7.17 illustrates the situation.

To provide for the distribution of work among the three service representatives and likewise to ensure that each person received service on a first-come-first-serve basis, Listing07_10 implements a queue. People get in line, and then the program directs them to an available service representative. The program ensures that the service representatives receive customers on an equally distributed basis.

CHAPTER 7

}

Arrays and Data Containers

174

Q Q Q TEAM LinG

Figure 7.17 A queue accumulates customer names for distribution among service representatives.

#Listing07_10

#Distribute customers among representatives using a queue

#1 Define a customer list

@Customers = qw (Ali Mo Stu Ed Ira Abe Todd Ham Si Lil Min Lo Dli Regi Cal Hal Su Len Joy Jay Xi Chi Tan Nu);

#Formatting

$LINE = "\n" . "=" x 60 ;

#Obtain the number of customers

$CustomerCount = scalar(@Customers);

#2 Distribute the customers

for($Itr = 0; $Itr < $CustomerCount; $Itr++){

#3 use the push function to push customer names into service queues push(@SallyCustomers, shift(@Customers));

push(@ChuCustomers, shift(@Customers));

push(@CaesarCustomers, shift(@Customers));

#Obtain a general number for the passengers

$ScheduledCustomers = scalar(@Customers);

Q Tracking Customers with a Queue

#4print the customers

print "$LINE \n \n Sally: \n \n " ;

for($Itr = 0; $Itr < scalar(@SallyCustomers); $Itr++){

print " \n " . $Itr + 1 . " " . $SallyCustomers[$Itr] . " ";

}

print "$LINE \n \n Chu: \n \n " ;

for($Itr = 0; $Itr < scalar(@ChuCustomers); $Itr++){

print " \n " . $Itr + 1 . " " . $ChuCustomers[$Itr] . " ";

}

print "$LINE \n \n Caesar: \n \n " ;

for($Itr = 0; $Itr < scalar(@CaesarCustomers); $Itr++){

print " \n " . $Itr + 1 . " " . $CaesarCustomers[$Itr] . " ";

}

print "$LINE";

print "\n \n \n";

#5 Check for whether to refresh the display if($ScheduledCustomers >= 3){

sleep(1.0);

print "\n \n";

system("cls");

# print "$LINE";

print "\n \n \n Thank you for your patience.";

}

#6 Exit when done

if($ScheduledCustomers == 0){

print "\n \n";

exit(0);

} }

CHAPTER 7

}

Arrays and Data Containers

176

Q Q Q TEAM LinG

In the lines that comment #1 in Listing07_10 designates, you create an array of customers called

@Customers. (This is the same customer group from Listing07_09.) You use the qw() func-tion in this case for convenience. In addifunc-tion to setting up the customer array, you also create a formatting line and use the scalar() function to obtain the number of elements in the

@Customers array and assign this to a scalar, $CustomerCount.

Next you turn to creating a for statement to attend to the primary activities of the program. In the lines accompanying comment #2, to control the for loop, you employ the value stored in

$CustomerCount. This approach to controlling the loop is a crude measure. You could, in fact, do without it.

Through the operations that the code affiliated with comment #3 performs, you use the push() function to push customers into three arrays you create to accommodate the three customer representatives. The shift() function extracts the names of customers from the front of the @Customer array. In this way, the customers who arrived first are given attention first.

Thepush() function inserts their names into the arrays that represent the customer service representatives.

Each customer service array serves as a queue. Each name is pushed into one of the customer service arrays from the back or end of the queue. It then migrates up the queue as other names arepush()ed into the queue.

In this instance, rather than using the shift() function to remove the name of the customer from the queue, you go only so far as to display the name of the customer in the order in which it entered the queue. To accomplish this, as you can see at comment #4, you employ the scalar form of each of the queues you have set up for the customer service representatives. You iterate through the scalar using a for loop controlled by the number of customers stored in the queues you have set up for the customer representatives. By using the scalar() function you are able to continuously monitor the number of customer names each queue stores.

The actions associated with comment #5 allow you to use the sleep() function to pause the display every second before refreshing it. You also make a call to the system() function, which allows you to issue commands to the operating system. In this instance, you issue the cls command, which clears the screen so the display of queued customers can be refreshed.

As a final measure, with the code introduced by comment #6, you check the number of customers (@SheduledCustomers) to see whether it has been reduced to zero. If so, you stop the action of the program. Figure 7.18 illustrates the output of Listing07_10.

Q Tracking Customers with a Queue

Figure 7.18 The program assigns customers to the service representatives.

Conclusion

In this chapter you have worked through several functions that allow you to view how the array can be used as a data container. Beginning with the reverse() and sort() functions, you explored the extent to which the elements in an array can be manipulated as a complete set.

Given a start with reversing and sorting arrays, you then moved on to examine how the split() function allows you to easily break the contents of strings into array elements. Along the same lines, you also dealt with the join() function, which you viewed in an earlier chapter in relation to string manipulations. In this chapter, the join() function allowed you to merge the contents of several arrays.

The step beyond merging arrays involved a set of four functions that allow you to make the array into a data container. These four functions consist of the shift(),unshift(),push(), and pop() functions. By using these functions with arrays in different ways, you can transform arrays into such data containers as stacks and queues. The uses of such data containers prove endless. In this chapter, you developed a simple stack program that allowed you to load pas-sengers into shuttles. You developed a simple queue program that allowed you to distribute the customers in a bank among three service representatives.

CHAPTER 7

}

Arrays and Data Containers

178

Q Q Q TEAM LinG

Hashes

8

This chapter introduces you to hashes. Hashes consist of containers that hold key-value pairs.

When you work with hashes, you associate a unique key with a value. The keys in any given hash must be unique, but the values need not be. When you add a duplicate key to a hash, the hash automatically replaces the old key with the new. Many basic functions allow you to work with hashes. Some of these are familiar if you have worked with Perl arrays. Others are new.

Among those familiar from working with arrays are the shift() and pop() functions. Among those new with hashes are the keys(),values(), and each() functions. The programs and activities presented in this chapter include the following:

Q Approaches to defining hashes Q Implicit and scalar forms of hashes

Q Keys and values in hashes and how to access them as arrays Q How to access both keys and values using the each() function Q Building tables to display the contents of hashes

Q Fetching values from hashes in specific orders

Dans le document This book is dedicated to its readers. (Page 195-200)

Documents relatifs