in reply to How do you read the next line in a file while in a loop?
Output:#!/usr/bin/perl -w use strict; my (%options,$curroption); while(<DATA>) { # read from __DATA__ filehandle below chomp; # get rid of trailing newlines next if /^\s*#/; # skip if it the first nonspace char is '#'. # set the curroption if the first item in the line is in all caps $curroption = $1 if s/^([A-Z]+) //; # make $currroption a hash key whose value is an array ref containin +g # the various options we found. push(@{$options{$curroption}},$_) for (split(/,\s*/)); } # print the datastructure which is a HoA (Hash of Arrays); for my $option (keys %options) { my @vals = @{$options{$option}}; print "$option -- ", join (',',@vals), "\n"; } __DATA__ # comment # comment ON Mon, Tues, Fri, Sat # comment other stuff
ON -- Mon,Tues,Fri,Sat,other stuff
-Blake
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: How do you read the next line in a file while in a loop?
by Hofmator (Curate) on Aug 31, 2001 at 12:56 UTC | |
|
Re: Re: How do you read the next line in a file while in a loop?
by GroundZero (Initiate) on Aug 31, 2001 at 03:00 UTC | |
by blakem (Monsignor) on Aug 31, 2001 at 03:04 UTC |