SPUG: Using regex rather than split for item sorting

Tim Maher tim at consultix-inc.com
Fri Mar 12 00:03:16 CST 2004


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.

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



More information about the spug-list mailing list