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.

Pereant, qui ante nos nostra dixerunt!

In reply to Re: New to Perl by Random_Walk
in thread New to Perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.