Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Automatic Subsequent Indexing.

by MiriamH (Novice)
on Jun 22, 2012 at 14:45 UTC ( [id://977855]=perlquestion: print w/replies, xml ) Need Help??

MiriamH has asked for the wisdom of the Perl Monks concerning the following question:

There are 15+ event titles that I want to capture. The event titles are always written between the words "summary" and "description". I indexed the words summary and description and then began printing out what was between the indexes, however I have been putting the index numbers in manually each time. Is there a way for Perl to automatically go to the next index? (Instead of $My Sum1 and $My Sum2 being specific numbers)

my $substr = 'SUMMARY'; my $offset = 0; my $result = index($string, $substr, $offset); while ($result != -1) { print "$result\n"; $offset = $result + 1; $result = index($string, $substr, $offset);} my $SUM1= "298"; my $SUM2="877"; my $length = $SUM2-$SUM1; my $fragment = substr $string, 298, $length; print " <$fragment>\n";

Replies are listed 'Best First'.
Re: Automatic Subsequent Indexing.
by muba (Priest) on Jun 22, 2012 at 15:28 UTC

    Something like this, you mean?

    use strict; use warnings; my $offset = 0; # Where are we in the string? my $string = do { # Grab the string. local $/; <DATA>; }; my $numResults = 0; while (1) { my $idxSummary = index($string, "SUMMARY", $offset); my $result = ""; if ($idxSummary > -1) { $offset = $idxSummary + length("SUMMARY"); my $idxDescription = index($string, "DESCRIPTION", $offset); if ($idxDescription == -1) { print "(Data malformed: missing DESCRIPTION line.)\n"; last; } my $length = $idxDescription - $offset; $result = substr($string, $offset, $length); $offset = $idxDescription + length("DESCRIPTION"); $result =~ s/^\s+|\s+$//g ; # Strip leading and trailing white +space, # includng newlines. $numResults++; } else { print "(All done. $numResults result(s) found.)\n"; last; } print " <$result>\n"; } __DATA__ This is bogus data SUMMARY Event 1 DESCRIPTION Lorem ipsum etc etc This is bogus data SUMMARY Event 2 DESCRIPTION Lorem ipsum This is bogus data SUMMARY The Third Event DESCRIPTION Lorem ipsum This is bogus data SUMMARY Event Number Four DESCRIPTION Lorem ipsum

    It gives this output:

    <Event 1> <Event 2> <The Third Event> <Event Number Four> (All done. 4 result(s) found.)

    Update: Alternatively, if it's not really the indexes you care about, but only capturing those titles, how about this?

    my @results = $string =~ m/SUMMARY\s*(.+?)\s*DESCRIPTION/g; print map { " <$_>\n"} @results;
      YOU ARE AMAZING!!! THANKYOU!!
Re: Automatic Subsequent Indexing.
by Anonymous Monk on Jun 22, 2012 at 15:14 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://977855]
Approved by johngg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 06:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found