A Perl Mystery

I’m trying to write a script to consolidate my book index, and I’ve run into a problem that’s driving me nuts. Can anyone see what’s going on here?

Here’s the relevant code:

if ($pagenumber) { #6
$numbers = $_;
$i = 0;

foreach (@numberarray) {
$numberarray[$i] = “”;
$i = $i + 1;
} # End foreach
$i = 0;

foreach (@sorted_numbers) {
$sorted_numbers[$i] = “”;
$i = $i + 1;
} # End foreach
$i = 0;

print $DEBUG2 “\$numbers is $numbers before entering while.\n”;

while ($numbers =~ /(\d+)(.*)/) {
print $DEBUG2 “\$1 is |$1|, \$pagenumber is |$pagenumber|.\n”;
if (!($1 eq $pagenumber)) {
$numberarray[$i] = $1;
$i = $i + 1;
} # End if
print $DEBUG2 “In while loop, \$numberarray[$i] is $numberarray[$i].\n”;
$numbers = $2;
print $DEBUG2 “Point 10: \$numbers is $numbers, \$i is |$i|.\n”;
} # End while
$numberarray[$i] = $pagenumber;
print $DEBUG2 “After while loop, \$numberarray[$i] is |$numberarray[$i]|, \$sorted_numbers[$i] is |$sorted_numbers[$i]|\n”;
@sorted_numbers = sort { $a <=> $b } @numberarray;
@numberarray = @sorted_numbers;
print $DEBUG2 “After sort, \$sorted_numbers[$i] is |$sorted_numbers[$i]|.\n”;

print $DEBUG2 “After sort \@numberarray is @numberarray, \$numberarray[$i] is $numberarray[$i].\n”;
$i = 0;
print $DEBUG2 “About to enter foreach (\@sorted_numbers).\n”;
$next = $i + 1; #Just for diagnostics
print $DEBUG2 “Before foreach, \$totalname is |$totalname|, \$numberarray[$i] is |$numberarray[$next]|, \$numberarray[$next] is |$numberarray[$i+1]|.\n”;
foreach (@numberarray) {
print $DEBUG2 “Got inside the loop.\n”;
print $DEBUG2 “**\$totalname is $totalname for \$i = $i.\n”;
$totalname = $totalname . ” ” . “$numberarray[$i],”;
$i = $i + 1;
}
print $DEBUG2 “Before chop condition, \$totalname is $totalname.\n”;
if ($totalname =~ /.*\,$/) {chop $totalname}
print $DEBUG2 “After chop condition, \$totalname is $totalname.\n”;

And here’s the debug output, for two different cases. One works, the other doesn’t and I can’t figure out what’s happening, but whatever it is, it seems to be happening in the sort. They should both give similar output — the name and a single page number, but as you can see, they don’t.

CASE 1

$numbers is Michael Adams 19, before entering while.
$1 is |19|, $pagenumber is |19|.
In while loop, $numberarray[0] is .
Point 10: $numbers is ,, $i is |0|.
After while loop, $numberarray[0] is |19|, $sorted_numbers[0] is ||
$sorted_numbers[0] is |19|.
after exiting @numberarray is 19, $numberarray[0] is 19.
About to enter foreach (@sorted_numbers).
Before foreach, $totalname is |Adams, Michael|, $numberarray[0] is ||, $numberarray[1] is ||.
Got inside the loop.
**$totalname is Adams, Michael for $i = 0.
Before chop condition, $totalname is Adams, Michael 19,.
After chop condition, $totalname is Adams, Michael 19.

CASE 2

$numbers is Bill Anders 26, before entering while.
$1 is |26|, $pagenumber is |26|.
In while loop, $numberarray[0] is .
Point 10: $numbers is ,, $i is |0|.
After while loop, $numberarray[0] is |26|, $sorted_numbers[0] is ||
$sorted_numbers[0] is ||.
after exiting @numberarray is 26, $numberarray[0] is .
About to enter foreach (@sorted_numbers).
Before foreach, $totalname is |Anders, Bill|, $numberarray[0] is ||, $numberarray[1] is ||.
Got inside the loop.
**$totalname is Anders, Bill for $i = 0.
Got inside the loop.
**$totalname is Anders, Bill , for $i = 1.
Got inside the loop.
**$totalname is Anders, Bill , , for $i = 2.
Before chop condition, $totalname is Anders, Bill , , 26,.
After chop condition, $totalname is Anders, Bill , , 26.

Things To Make You Feel Old

What’s funny about this is that most of them don’t have that effect at all on me, or at least not in the intended sense. They only make me feel old in the sense that many of them are and always have been irrelevant to my life, and many are in fact completely unknown to me. They’re just a bunch of things to make a young person feel old. You’d need a much different set of things to make me feel old.

The Safety Of SLS

Steven Squyres is concerned.

Here’s what I wrote in the book on that topic:

It should be noted that NASA currently plans only two flights for the SLS — one in 2017 to demonstrate the 70-ton capability, and one with a crew in 2021, to… somewhere. They have said that, when operational, it may only fly every couple of years. What are the implications of that, in terms of both cost and safety?

Cost wise, it means that each flight will cost several billion dollars, at least for those first two flights. If, once in operation, it has a two- or three-billion-dollar annual budget (a reasonable guess based on Shuttle history), and it only flies every couple of years, that means that each subsequent flight will cost anywhere from four to six billion dollars.

From a safety standpoint, it means that its operating tempo will be far too slow, and its flights far too infrequent, to safely and reliably operate the system. The launch crews will be sitting around for months with little to do, and by the time the next launch occurs they’ll have forgotten how to do it, if they haven’t left from sheer boredom to seek another job.

As a last-ditch effort to try to preserve the Shuttle in 2010, some suggested that it be maintained until we had a replacement, but to fly it only once per year to save money. The worst part of such a proposal would have been the degree to which the system would have been even less safe, given that it was designed for a launch rate of at least four flights per year. It was unsafe to fly it too often (as NASA learned in the 80s as it ramped up the flight rate before Challenger), and it would be equally so to fly it too rarely. NASA’s nominal plans for SLS compound this folly, which is magnified by the fact that both internal NASA studies and independent industry ones have demonstrated that there is no need for such a vehicle to explore beyond earth orbit (existing launchers could do that job just fine, with orbital mating and operations), and it is eating up all the funding for systems, such as landers and orbital propellant storage facilities, that are necessary. All of this is just more indication that actually accomplishing things in space is the lowest priority for Congress (and unfortunately, the space agency itself, otherwise, the administrator would be more honest with the appropriators on the Hill).

More people need to point this out.

Biting Commentary about Infinity…and Beyond!