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

Tim Maher tim at consultix-inc.com
Fri Mar 12 12:05:02 CST 2004


On Fri, Mar 12, 2004 at 09:40:30AM -0800, Scott Blachowicz wrote:
> 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

Arrgh!  Wrong nonsense character.  8-}

A cleaned-up version that actually works is shown below.

Thanks Scott!

*--------------------------------------------------------------------------*
| 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_items
# Tim Maher, tim at teachmeperl.com
# Thu Mar 11 21:13:40 PST 2004

# Quick and dirty program to sort POD list items into
# ASCIIbetical order. Doesn't handle nested lists yet


{	# Local change to $/
	local $/=undef;

	# NOTE: Uses file-slurping mode
	# $_=<DATA>;	# For test runs
	$_=<>;
}

# First, extract each '=item' label from the POD-list along wtih
# its following data

@items = /^(=item\b.*?)(?=^=item\b|\Z)/smg;

# Print chunks to see if it came out correctly
$ENV{DEBUG} and do {
	$i=1;
	foreach ( @items ) {
		print "$i: $_\n";
		$i++;
	}
};
    
# Now use Schwartzian transform to sort list-items according to
# their labels, and print

print
  map { $_->[1] }
    sort { $a->[0] cmp $b->[0] }
	map {
		 if ( /^=item\s+([^\n]+)\n/ ) {	# $1 is label for list item
			[ $1 , $_ ]
		 } else {
			die "$0: Bad data: List items must start with =item\n";
		 }
	} @items;

__DATA__
=item This

and stuff was written

=item What

more drivel here

=item Other

getting the idea?

=item That's all

the end





More information about the spug-list mailing list