CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Database structure

 



socr85bling
New User

Mar 27, 2002, 9:48 PM

Post #1 of 6 (919 views)
Database structure Can't Post

I am making a site right now that could have some huge logs, so I need to know how to replace a line in a log file without rewriting the WHOLE file. Here's what it looks like:



name1§name2§name3§name4§name5§name6§name7§

email1§email2§email3§email4§email5§email6§email7§

other1§other2§other3§other4§other5§other6§other7§



Now, I can store that as an array and no matter how many names i get i can always split them up into substrings b/c of that delimiter(§). The line with names would be $array[0] , the line with email would be $array[1], you know what im saying... anyway, instead of replacing an element in the array with an appended value then rewriting the entire log file, how can i JUST append to ONE line in the log(IE stick an email address after the last email on that line)

I know after this file gets to be pretty large it will start taking forever to rewrite the whole thing, thats my reason for wanting to do this - thanks for reading and any help would be appreciated =)


PerlKid
stranger

Mar 27, 2002, 10:42 PM

Post #2 of 6 (917 views)
Re: [socr85bling] Database structure [In reply to] Can't Post

can't each record in the log be on a seperate line?. That would make appending/modifying much easier and memory efficient (since you could loop through on the filehandle rather than slurping the file into nested arrays).
--Philip
FuzzyLogic at PerlMad dot com


socr85bling
New User

Mar 28, 2002, 5:14 AM

Post #3 of 6 (913 views)
Re: [PerlKid] Database structure [In reply to] Can't Post

I don't know if I understand what you're saying... are you saying I should put each element on one line, and for() loop with $scalar = <LOG>; through the last piece of info in the log?

If I remember correctly, I couldn't use that before(I would have to get the <LOG> as an array, even if it was one piece on info). Of course I could have done something wrong, b/c I think I only tried it once. Anyway, even if I do it that way, how can I JUST append a piece of info on the line after, say, the 34th name without having to rewrite the whole thing each time?(since that would start taking minutes once it gets big)

Thanks in advance


PerlKid
stranger

Mar 28, 2002, 11:02 AM

Post #4 of 6 (906 views)
Re: [socr85bling] Database structure [In reply to] Can't Post

Okay, how about this... are name1, email1, and other1 related to the same record? If so, then you should do:
name1|email1|other1
name2|email2|other2
etc..
--Philip
FuzzyLogic at PerlMad dot com


socr85bling
New User

Mar 28, 2002, 1:35 PM

Post #5 of 6 (901 views)
Re: [PerlKid] Database structure [In reply to] Can't Post

What happens if there gets to be more than 255 names/emails/others? I won't be able to store the log as an array, but I still might be able to use $scalar=<LOG>; to get the current element(haven't tried it yet)


PerlKid
stranger

Mar 28, 2002, 1:54 PM

Post #6 of 6 (899 views)
Re: [socr85bling] Database structure [In reply to] Can't Post

do you need to suck the whole log file into memory? Or just parts? Arrays are not limited to 255 elements so that's a non-issue. What you're looking at is memory consumption and speed. Simply loop on the file handle, take what you need from the record and store than in your array and discard the rest. Arrays of hashes are nice if you need all data from every record.

For example, if you only wanted email addresses, then you could do this:

Code
my @fields = qw(name email other); 
my @emails = ();
open (DAT, "data.db") or die $!;
while (<DAT>) {
chomp;
my %rec;
@rec{@fields} = split /\|/;
push @emails, $rec{email};
}
close DAT;
$, = "\n";
print, foreach (sort @emails);

which loops over the data, extracting only the emails, discards the rest, sorts the emails, and prints them out.
--Philip
FuzzyLogic at PerlMad dot com

(This post was edited by PerlKid on Mar 28, 2002, 1:59 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives