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

Dear Monks,
I have a great challenge for you to show your regexp and/or other perl skills. I am working on a text file parser, which is importing records into a database. Each row is a document which have a chapter list identifier, like 1.1. or 2.4.5.6
When i am reading a row, i want to know the prior chapter to the current one. For example 1.1.'s prior is 1., 2.4.5.6's is 2.4.5.
So the question is if how you dig it out from the identifier. I have a solution, (so it is not a homework that i want to be solved by others), but I would like to see your faster, more clever, and more obfuscated solutions! So chapter up!

My solution:

if ($chapter=~/([\d+\.]+)\d+\.?/) { print "$1 is prior to $chapter"; }
As you see it is not sure if the point is presented at the end of the chapter identifier or not ( \.? )

--
tune

Replies are listed 'Best First'.
Re: Parsing chapter id's
by Masem (Monsignor) on May 05, 2001 at 15:26 UTC
    Split is your friend:
    my @chapters = split /\./, $chapter; pop @chapters; my $prev_chapter = join '.' @chapters;
    (I'm sure this can be golfed, but this is for clarity now :D)


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: Parsing chapter id's
by chipmunk (Parson) on May 05, 2001 at 19:43 UTC
    I think that the key is matching everything up to the last period:
    if ($chapter =~ /^([\d.]+\.)\d+$/) { print "$1 is prior to $chapter.\n"; }