This is not pretty but it is functional. I missed out the speaking order part and the seperation of reading from outputing makes the code far more complex than required. I will try to make a better example later, but I've written it so here goes.
#!/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; # OK no lets print out what we got foreach my $href_case (@cases) { my $case_number=$href_case->{case_number}; my $seat_number=1; foreach my $player ( @{$href_case->{seat}} ) { my @output; push @output, $player, $case_number, $seat_number++ ; my $round=0; foreach ( @{$href_case->{round}} ) { push @output, "round_".($round + 1); for my $answer_no (0..4) { my $answer; if (defined $href_case->{$player}[$round][$answer_no]) + { $answer=$href_case->{$player}[$round][$answer_no]; } else { $answer=0; } push @output, $answer; } $round++; } print join ",",@output; print "\n"; } } __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 </readmore><readmore> # output like this Joe,12345,1,round_1,says bad,0,0,0,0,round_2,says bad,0,0,0,0 Steve,12345,2,round_1,says good,says that's enough,0,0,0,round_2,says +bad,0,0,0,0 Mary,12345,3,round_1,doesn't talk,0,0,0,0,round_2,doesn't talk,0,0,0,0 Jill,12345,4,round_1,says good,says that's enough,0,0,0,round_2,says b +ad,0,0,0,0 Bob,12345,5,round_1,doesn't talk,0,0,0,0,round_2,doesn't talk,says tha +t's enough,0,0,0
Cheers,
R.
In reply to Re^2: New to Perl
by Random_Walk
in thread New to Perl
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |