From: zudew@cerridwen.ucs.orst.edu (Bill Zude)
Subject: Another new programmer cries for HELP!
Summary: Pascal-like case statement and general help 
Organization: University Computing Services, OSU
Date: Fri, 9 Oct 1992 23:32:01 GMT

# I'm an extremely novice programmer working in the Computing
#  Services department as a part-time assistant system 
#  administrator.  My FIRST tasks are to modify and create
#  numerous perl scripts to extract and compile statistical data
#  from various system usage log files.  There are currently no 
#  perl experts around campus (that I know of) so I NEED HELP!  
# Any response regarding the sample script below on approach,
#  implementation, efficiency, why the case subroutine 
#  from the Camel book will not compile, etc. would be greatly
#  appreciated.  I will check my email frequently for messages
#  from anyone who choses to respond to my email address given 
#  below. 
# THANK YOU! 
#
# Script to extract statistical data from newsgroup access
#  log file. 
# Subroutine does not compile.  Receive "EOF in string" at
#  first line of subroutine call
#
#!/usr/local/bin/perl
 
eval "exec /usr/local/bin/perl -S $0 $*"
    if $running_under_some_shell;
			# this emulates #! processing on NIH machines.
			# (remove #! line above if indigestible)

eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_]+=)(.*)/ && shift;
			# process any FOO=bar switches


while (<>) {
	chop;
	if (/: Accepted connection from:/) {
		@tmp=split(' ',$_);
		if ( $#tmp == 15) { 
    		($pid,$seq,$month,$day,$time,$year,$somecode,$cmd,$str1,$str2,$ip,@str3) = split(' ', $_);
			chop($ip);
			$hostconn{$ip}++;
			$conn{$time}++;
			$conn{'numconn'}++;
			print "Accepted\n" if $opt_v;
		} else { print "Invalid format: $_\n" if $opt_v;; }
	}
} 

# Number of hosts that connected more than once
	
	sub case {
		local(*assoc,$_) = @_;
		for (split(/\n/)) {
			/^(\S+)\s+(.*)/;
			for (eval $1) {
				$assoc{$_} = $2;
			}
		}
	}

	&case(*foo,<<'ENDCASE');
		1			$conngt{'1'}++;
		2..4		$conngt{'2'}++;
		5..9		$conngt{'5'}++;
		10..49		$conngt{'10'}++;
		50..99		$conngt{'50'}++;
		100..199	$conngt{'100'}++;
		200..299	$conngt{'200'}++;
		300..399	$conngt{'300'}++;
		400..499	$conngt{'400'}++;
		500..999	$conngt{'500'}++;
		1000..9999	$conngt{'1000'}++;
	ENDCASE

	foreach $ip (keys %hostconn)  { 
		eval $foo{$ip};
	}

format top =


	HOSTNAME                              #CONNECTIONS
	------------------------------------- ------------
.
format STDOUT =
	@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @>>>
	$hostname,								@tmp[2]
.
open(TMP,">$tmpsort");
foreach $g (keys %hostconn) { printf TMP $fmt, $g, $hostconn{$g}; }
close(TMP);
system("/bin/sort -nbr +1.0 -2.0 -o $tmpsort $tmpsort");
open(TMP,"$tmpsort");
while (<TMP>) { 
	chop;
	@tmp=split(/ /,$_);
	system("dig -x @tmp[1] > tmp/DIGOUT");
	$digout = "tmp/DIGOUT";
	open(DIG, $digout) || die "Cannot open $file: $!";
	$hostname = "";
	while (<DIG>) {
		chop;
		if (/;; ANSWERS:/) {
			($ipaddr,$size,$class,$hostname) = split(' ', <DIG>);
			chop($hostname);
			write;
		} 
		elsif ((/;; AUTHORITY RECORDS:/) && ($hostname eq "")) {
			($arpa,$size,$type,$SOAname,@str1) = split(' ', <DIG>);
			chop($SOAname);
			$tag = "SOA Name: ";
			$hostname = $tag . $SOAname;
			write;
		}
	}
	close(DIG);
	unlink($digout);
}
close(TMP);
unlink($tmpsort);



*************************************************************************
|    William Zude                 Internet: zudew@cerridwen.ucs.orst.edu|
|    University Computing Services                                      |
|    Oregon State University         Phone: (503) 737-4482              |  
|    Corvallis, OR 97331-5202                                           | 
*************************************************************************