Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Parsing chapter id's

by tune (Curate)
on May 05, 2001 at 08:50 UTC ( [id://78195]=perlquestion: print w/replies, xml ) Need Help??

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"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-19 20:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found