On 8/28/07, Jeff Pang <[EMAIL PROTECTED]> wrote: > 2007/8/28, Chris <[EMAIL PROTECTED]>: > > Yet another problem from Intermediate Perl, I've been given a log > > file. Here is a sample of its contents: > > > > Gilligan: 1 coconut > > Skipper: 3 coconuts > > Gilligan: 1 banana > > Professor: 3 coconuts > > MaryAnn: 2 papayas > > Thurston: 2 mangoes > > > > I am supposed to read the log file and output its contents into a > > series of files called gilligan.info, maryann.info, etc. In the end I > > should I have a .info file for each character. In the file will be > > information pertaining to the character. For example, gilligan.info > > will have > > Gilligan: 1 coconut > > Gilligan: 1 banana > > > > while (<>) { > my ($f) = (split)[0]; > $f=~s/://; > $f=lc($f); > open FD,'>>',"$f.info" or die $!; > print FD $_; > close FD; > }
Why run two regexes against the line? Also, you don't need the subscript (sine it is the first element). my ($f) = split /: /; You can also simplify the whole thing to perl -MIO::All -F': ' -ane 'io(lc "$F[0].info")->append($_)' But the purpose of the exercise is not to write the program with the least number of statements, it is to learn how to use hashes and file handles effectively (both of our examples are wastefully reopening the file). -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/