Questions for Chapter 10

Q:

Does a sort always have to be alphabetical? (if not how can you sort based on other attributes)

A:

As far as I know perl only does by alpha. There used to be a function called bynumber but it seems to have disappeared. You could always write your own sub routine and stick it in between to do what you want.

Q:

Can you sort values instead of just the subscripts? (I don't really understand the whole subscript thing anyway)

A:

#!/usr/local/bin/perl

%chararray = ("thom", "3", "bob", "1", "chris", "2");

foreach $word (sort keys(%chararray)) {
	print "$word\n";
}

foreach $word (sort values(%chararray)) {
        print "$word\n";
}

#### Output
# > perl sortnum.pl
# bob
# chris
# thom
# 1
# 2
# 3

Q:

? Why don't the values and subscripts stay in order? What is the computer doing that causes them to get out of order?

A:

They are not stored in order. They are 'hashed' which means there is an algorithm which is used to put the values in a special look table based on a combination of the key and the value. They are combined usually in a linked list based on some rules. I have no idea what rules perl uses for its tables.

Q:

Linked lists? I didn't read a whole lot of this because I wanted to wait until I got some of my other questions answered and had a better understanding of associative arrays before confusing myself even more.

A:

The example they use is a linked list abel, baker, baker, charlie, charlie delta, delta, "" where there is a variable $header = "abel" which means $list{$header} will be abel which has the value of baker which is the key for the next data element.

Q:

Is this chapter suggesting that variable arrays are in fact listed: 0, Sam, 1, Sonja, 2, Cait, 3, Ritu ?

A:

#!/usr/local/bin/perl

%chararray = ("0", "Sam", "1", "Sonja", "2", "Cait", "3", "Ritu");

foreach $word (sort keys(%chararray)) {
	print "$word\n";
}

foreach $word (sort values(%chararray)) {
	print "$word\n";
}

###### Output
# > perl sortTerry.pl
# 0
# 1
# 2
# 3
# Cait
# Ritu
# Sam
# Sonja

Q:

If this is the case, it would finally explain to me how @class; can simultaneously give the value "3" and "Sam, Sonja, Cait, Ritu".

This has been a riddle haunting me for awhile.

A:

It is now haunting me cause I have no idea what you are talking about.

Q:

Although I'm not completely clear on linked lists and trees, my question is closer to the beginning of chapter 10: If array elements that we've been working with can print elements within that array in order, and associative array elements can't be recalled in order without using the "sort" function, then will an associaive array stored within a regular array recall them in order?

A:

They will be recalled in order is you do something such as this:

#!/usr/local/bin/perl

%chararray = ("Sym", "Sonja", "Cait", "Ritu");

@array = sort %chararray;

for ($ctr=0; $ctr < @array ; $ctr++) {
	print "$array[$ctr]\n";
}

##### Output
# > perl naomi.pl
# Cait
# Ritu
# Sonja
# Sym

Q:

Also, I'm still notclear on the "keys" function. I know it retrieves a list of subscripts used in associative arrays, and "value" retrieves the value of each element. How come page 334, listing 10.3 doesn't have to ask for "value" in line 6 in order to print the value?

A:

print ("Number of bannas: $fruit{\"bananas\"}\n");

$fruit{"bananas"} is asking for the value.

Can the "each" statement be used there...such as, while(($fruit, $value) = each(%fruit)) { print...etc.}

A:

#!/usr/local/bin/perl

%fruit = ("Sym", "Sonja", "Cait", "Ritu");

while(($fruit, $value) = each(%fruit)) { 
	print "$fruit => $value \n";
}

###### Output
# > perl naomi.pl
# Sym => Sonja
# Cait => Ritu

Q:

Can only numbers be put in associative arrays? the explanation of listing array indexes and values made me wonder. He say "keys" is for the subscripts, which are words, but that "values" will separate out the values, are these only numerical values?

A:

Look at the examples above.

Q:

Looping using an associative array...this whole foreach looping thing is sort of confusing.

A:

So was driving when you started out. Once you get some practice it will become second nature.

Q:

Does the "each" function randomly put together the the two-element lists? If so, how is this useful?

A:

It will give you all the content of the associate array. You then do something with this content like search and replace.

Q:

Listing 10.1 at page 328, line 7. Where do we take @wordlist from? How do we know it's length when we compare $count with it?

A:

I discuss this further down the page.

Q:

Listing 10.3 at page 334, line 6. Why does $fruit{\"bananas\"} have back slashes in it? I tried to run the program without them, and it does not work. What do back slashes do?

A:

You left out context in your example. The complete context is:

print ("Number of bananas: %fruit{\"bananas\"}\n");

We want to do "bananas" but can't because "bananas" is already in a string which starts and ends in " " The backslashes tells perl to ignore the quotes as quotes so it doesn't match the starting quote.

Q:

I could understand 'key' and 'value' part of associative array. But not the delete and addition. Also in the very first example 10.1, My program omits capital words instead of printing them?

 while  ($inputline = <STDIN>) {
	while ($inputline =~ /\b[a-z]\S+/g) {
		$word =$&;
		$word =~ s/[;.,:-]$//; #remove punctuation
		for ($count =1; $count <= @wordlist; $count ++) {
			$found = 0;
		if ($wordlist[$count-1] eq $word) {
			$found =1;
			$wordcount[$count -1] +=1;
			last;
		}
	}
	if ($found == 0) {
		$oldlength = @wordlist;
		$wordlist[$oldlength] =$word;
		$wordcount[$oldlength] =1;
		}
	}
}

print ("Capitalized and number of occurrences:\n");
for ($count =1; $count <= @wordlist; $count ++) {
	print ("$wordlist[$count -1]: $wordcount[$count -1]\n");
}

A:

You typed the wrong line here:

while ($inputline =~ /\b[a-z]\S+/g)

but look at this example:

#!/usr/local/bin/perl
print "Enter a line\n";
chop($inputline = <STDIN>);
while  ($inputline ne "") {

	# if the $inputline contains a Capital at the beginning
        while ($inputline =~ /\b[A-Z]\S+/g) {
                $word =$&;
                $word =~ s/[;.,:-]$//; #remove punctuation

		# this for loop _only_ executes if there is something
		# in @wordlist. Otherwise it drops thru to the while loop
                for ($count =1; $count <= @wordlist; $count ++) {
                        $found = 0;
			# you find the word in the list, increment the count
                	if ($wordlist[$count-1] eq $word) {
                        	$found =1;
                        	$wordcount[$count -1] +=1;
                        	last;
                	}
        	}

		# If you didn't find the word, it is new, add to list
        	if ($found == 0) {
                	$oldlength = @wordlist;
                	$wordlist[$oldlength] =$word;
                	$wordcount[$oldlength] =1;
                }
        }
print "Enter a line\n";
chop($inputline = );
}

print ("Capitalized and number of occurrences:\n");
for ($count =1; $count <= @wordlist; $count ++) {
        print ("$wordlist[$count -1]: $wordcount[$count -1]\n");
}