SPUG: Re: Using regex rather than split for item sorting

Scott Blachowicz scott+spug at mail.dsab.rresearch.com
Fri Mar 12 11:40:30 CST 2004


Tim Maher <tim at consultix-inc.com> wrote:

> I tried to convert my earlier unappealing split()-based solution
> to one that uses a matching operator, but I guess I've bungled the
> postive-lookahead somehow, because it doesn't extract the last
> label/item pair.

Positive-lookahead is '?=', not '?:'...

Scott

> 
> Can somebody see the problem?
> *--------------------------------------------------------------------------*
> | Tim Maher, CEO     (206) 781-UNIX      (866) DOC-PERL     (866) DOC-UNIX |
> | tim(AT)Consultix-Inc.Com  http://TeachMePerl.Com  http://TeachMeUnix.Com |
> *+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*
> |  Classes: Perl Prog. plus Perl Modules, 4/5-4/8 ;  Template Toolkit: TBA |
> |  Watch for my Manning book: "Minimal Perl for Shell Users & Programmers" |
> *--------------------------------------------------------------------------*
> 
> #! /usr/bin/perl -wl
> # sort_pod_items2
> # Tim Maher, tim at teachmeperl.com
> # Thu Mar 11 21:59:09 PST 2004
> 
> # Quick and dirty program to sort POD list items into ASCIIbetical order
> # Doesn't handle nested lists yet, and is in dire need of a more elegant way
> # to get item-labels hooked together with their contents
> 
> # NOTE: Runds in file-slurping mode, so all data presented at once to implicit loop
> 
> $/=undef;
> $_=<DATA>;
> 
> # First, split lines into '=item' labels for POD-list items,
> # followed by associated contents
> 
> # Something wrong with following; doesn't extract last list item
> @fields= /^(=item\b.*?)(?:^=item\b|\Z)/smg;
> 
> $i=1;
> foreach ( @fields ) {
> 	print "$i: $_\n";
> 	$i++;
> }
> exit;
> 
> 
> print map { $_->[1] }
>  sort { $a->[0] cmp $b->[0] }
>  map {
> 	 if ( $_ =~ /^=item\s+([^\n]+)\n/ ) {	# $1 is label for list item
>  		[ $1 , $_ ]
> 	 } else {
> 	 	die "Bad data: records must start with =item\n";
> 	 }
>  } @fields;
> 
>  __DATA__
> =item This
> 
> and stuff was written
> 
> =item What
> 
> more drivel here
> 
> =item Other
> 
> getting the idea?
> 
> =item That's all
> 
> the end
> _____________________________________________________________
> Seattle Perl Users Group Mailing List  
> POST TO: spug-list at mail.pm.org  http://spugwiki.perlocity.org
> ACCOUNT CONFIG: http://mail.pm.org/mailman/listinfo/spug-list
> MEETINGS: 3rd Tuesdays, Location Unknown
> WEB PAGE: http://www.seattleperl.org

Scott.Blachowicz at seaslug.org



More information about the spug-list mailing list