 # CSCI A348/A548 Lab Notes 9

Fall 1999

This lab summarizes a few notes from yesterday's lecture for your midterm practice. Problems 7 and 10 in homework assignment 6 are related. The second one (problem 10) refers directly to the example below, taken directly from lecture notes 11.

```<SCRIPT>
var _console = null;

function debug(msg)
{
// Open a window the first time we are called, or
// after an existing console window has been closed.
if ((_console == null) || (_console.closed)) {
_console = window.open("","console",
"width=600,height=300,resizable");
// Open a document in the window to display plain text.
_console.document.open("text/plain");
}

_console.document.writeln(msg);
}
</SCRIPT>

<!-- Here's an example of using this script. -->
<SCRIPT>var n = 0;</SCRIPT>
<FORM>
<INPUT TYPE="button" VALUE="Push Me"
onClick="debug('You have pushed me:\t' +
++n + ' times.');">
</FORM>```
If you haven't tried it out please do so. It should be immediate how one could add buttons with functions and create a primitive calculator out of this example.

Reviewing lecture notes 11, and 13-14 is likely to be very helpful as far as JavaScript goes. Same for the chapter in the book and the 'shopping cart' aplication (homework assignment 5). One of the questions on the JavaScript quiz looked like this (although not exactly). What does the following produce and why?

```function b(x, y) {
return x * y;
}
function a(x, y) {
return x(y, 5);
}
document.write(a(b, b(2, 3))); ``` Here's the very short version of a program that looks like the one that we developed in the lecture last night. It reads a file and processes every line such that anywhere where a digit appears it rewrites it as a number bigger with `1`. The program is called `inflation` and here it is together with a sample text file used as a test (`mytext`):
```school.cs.indiana.edu%cat inflation
#!/usr/bin/perl

open (AB, \$ARGV);
while (\$x = <AB>) {
\$x =~ s/(\d)/\$1 + 1/ge;
print \$x;
}
close(AB);

school.cs.indiana.edu%cat mytext
This file has 12 numbers 34 in it 0
such that 98 I can 4 identify 56 them
all and 6 produce inflation. 45

school.cs.indiana.edu%./inflation mytext
This file has 23 numbers 45 in it 1
such that 109 I can 5 identify 67 them
all and 7 produce inflation. 56

school.cs.indiana.edu%```
It's easy to change this program such that it turns all vowels into uppercase. I would also recommend that you try to make a change in the program above such that the digits after having been incremented by 1 appear surrounded by parentheses in the output.

Something like this:

```school.cs.indiana.edu%cat mytext
This file has 12 numbers 34 in it 0
such that 98 I can 4 identify 56 them
all and 6 produce inflation. 45

school.cs.indiana.edu%./inflation mytext
This file has (2)(3) numbers (4)(5) in it (1)
such that (10)(9) I can (5) identify (6)(7) them
all and (7) produce inflation. (5)(6)

school.cs.indiana.edu%``` Here's a variation on this in which we extract substrings that match a certain pattern (by identifying them and removing them from the original string) and do something with them. We do each substitution once so we need to use a `while` loop to find all substrings (that would otherwise be achieved by using a `g` modifier).

Here's the program, test it on the `mytext` file.

```#!/usr/bin/perl

open (AB, \$ARGV);
@lines = <AB>;
\$text = join('', @lines);
while (\$text =~ s/(\d+)//) {
\$i++;
print "\$i. \$1\n";
}
print "\n--------( remaining text )---------\n\$text";
close(AB);```