Re: Request for help

David L. Sifry (david@sifry.com)
Wed, 24 Jul 1996 02:02:23 -0700


guestXX wrote:
> 
> 
> We are
> reading a log file, comprising of a few hundred lines of data in a 
> uniform format. We have been able to parse it into it's various 
> components, but are not being able to store it into arrays. 
... 
> The crux of the problem lies in the fact that we do not know how to 
> preextend the arrays which are going to get big. 

If I'm understanding you correctly (sample code is always useful when
describing a problem, BTW)  you can read in the database line-by-line,
but you can't store it in an array?

Try using associative arrays - see pages 29-32 of the camel book for a
brief description.  The beauty is that perl handles adding new index
elements to the arrays.

For example, if you've got a log in the format of:

date	time	IP address	etc

separated by white space (with no white space in each field itself),
here's a snippet of code (note, I'm just typing this off the top of my
head at 1:40AM, so I won't vouch for its accuracy!):

$loop = 1;
while (<FILE>) {
  chop;
  ($date{$loop},$time{$loop},$ip{$loop},$etc{$loop}) = split(/ /);
  $loop++;
}

That's about as boring a way as you can do it in perl.

Much more fun would be to keep track of how many times a certain IP
address shows up in the database:

while (<FILE>) {
  chop;
  ($date,$time,$ip,$etc) = split(/ /);
  $accesses{$ip}++;
}

Anyway, hope that helps.  2 pieces of free advice:  #1: Read the Camel
book (that's "Programming Perl" by Larry Wall and Randal L. Schwartz)
and #2: read the camel book. :-)  Enjoy getting to know perl!

You also might find the comp.lang.perl.* newsgroups extremely helpful.

Well, I'm off to bed.

Dave
-- 
Dave Sifry
david@sifry.com, sifry@aptltd.com
(408) 471-0667 (voice)
(408) 471-0666 (fax)