in reply to New to Perl
First off welcome to perl, I hope you enjoy your stay.
As others have said your output requirements are rather vague so what I have done is parsed your input into a perl data structure. This I have just dumped out so you can see that we have all the data. You can really pick and choose what you want and how you want to format the output.
I have made the assumption that the format you gave is fairly complete, e.g. what they say will always contain says or doesn't speak. I have allowed names to contain whitespace and funny characters. if there are certain to be simple then they can be better matched with (\w+). You mention each round having a question but I do not really see this in your data. I have captured the bit after the round number as a guess of where I find this (round 2 has a value here, round 1 not)
click the read more links to open up the code underneath
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @cases; my %current_case; my $current_round; while (<DATA>) { next if /^\s*$/; # ignore blank lines if (/#Case Number: (\d+)/) { %current_case && push @cases, \%current_case; %current_case=(); $current_case{case_number}=$1; $current_case{seat}=[]; $current_case{round}=[]; } elsif (/People at table = (\d+)/) { $current_case{people_at_table}=$1 } elsif (/Seat (\d+): (.*)/) { # perl starts counting from zero so we decrement seat number my $seat = $1 - 1; $current_case{seat}[$seat]=$2; $current_case{$2}=[]; } elsif (/(.*) speaks first/) { $current_case{speaks_first}=$1; } elsif (/Round (\d+):(.*)/) { $current_round=$1-1; # perl likes to start counting at zero $current_case{round}[$current_round]=$2 ? $2 : ""; } elsif (/(.*) ((says|doesn\'t talk).*)/) { $current_case{$1}[$current_round]=[] unless defined $current_c +ase{$1}[$current_round]; push @{$current_case{$1}[$current_round]}, $2; } } push @cases, \%current_case; print Dumper(\@cases); __DATA__ #Case Number: 12345 People at table = 5 Seat 1: Joe Seat 2: Steve Seat 3: Mary Seat 4: Jill Seat 5: Bob Jill speaks first Round 1: Jill says good Bob doesn't talk Joe says bad Steve says good Mary doesn't talk Jill says that's enough Steve says that's enough Round 2: Next question Jill says bad Bob doesn't talk Joe says bad Steve says bad Mary doesn't talk Bob says that's enough # That is the script, this is the output $ ./case $VAR1 = [ { 'people_at_table' => '5', 'round' => [ '', ' Next question' ], 'seat' => [ 'Joe', 'Steve', 'Mary', 'Jill', 'Bob' ], 'case_number' => '12345', 'Joe' => [ [ 'says bad' ], [ 'says bad' ] ], 'Bob' => [ [ 'doesn\'t talk' ], [ 'doesn\'t talk', 'says that\'s enough' ] ], 'Jill' => [ [ 'says good', 'says that\'s enough' ], [ 'says bad' ] ], 'Mary' => [ [ 'doesn\'t talk' ], [ 'doesn\'t talk' ] ], 'speaks_first' => 'Jill', 'Steve' => [ [ 'says good', 'says that\'s enough' ], [ 'says bad' ] ] } ]; $
Cheers,
R.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: New to Perl
by chromatic (Archbishop) on Jan 20, 2005 at 23:58 UTC | |
by Random_Walk (Prior) on Jan 21, 2005 at 01:02 UTC | |
by Random_Walk (Prior) on Jan 21, 2005 at 14:22 UTC | |
|
Re^2: New to Perl
by Anonymous Monk on Jan 20, 2005 at 18:31 UTC | |
by Random_Walk (Prior) on Jan 20, 2005 at 18:42 UTC | |
by renz (Scribe) on Jan 20, 2005 at 19:49 UTC | |
by tall_man (Parson) on Jan 20, 2005 at 22:59 UTC |