A better way . . .

mikeraz at patch.com mikeraz at patch.com
Fri Dec 14 13:28:30 CST 2001


On another list someone said:

> I'm hacking on a script... and in that script, there's a list of
> services that looks like this:
>
>SERVICES="mysql postgresql bind9 jabber nfs-kernel-server nfs-user-server \
>  samba apache ssh ntp inetd exim sendmail"
>
>  I'm the type of pedantic individual who likes lists like that to be
>  sorted in alphabetical order, so here's what I did[1]:
>
> [all of his emacs buffer opening, and lisp example deleted]

And someone replied with:

> (From the start of the line in command mode)
> 
> "adf"   <--- Delete until a " and stuff it into buffer a
> $"bx    <--- Go to the end of the line and delete the other ",
>              save in buffer b
> ".!perl -pe 'tr/ /\n/;' | sort | perl -pe 'tr/\n/ /;'
>         <--- sort the line using sort.
> ^"aP    <--- put the leading stuff back
> $"bp    <--- put the trailing stuff back
>
> Someone out there will know how to do this without deleting the leading
> and trailing stuff with a mark or something.

To which I replied:


Why yes, someone would:

:.!perl -ne '(@i)=split /\"/; (@w)=split / /, $i[1]; print "$i[0]\"", (join " ", sort @w), "\"";' 

	or
:.!           # send current line to 
perl -ne '    #perl treating the following string as the contents of a while(<>){ ... } loop
(@i)=split /\"/;       # cut the line up based on quotes
(@w)=split / /, $i[1]; # cut the stuff up between the quotes based on spaces
print "$i[0]\"",       # print the begining of the line and the opening quote
(join " ", sort @w),   # and the sorted space reinserted data
 "\"";'                # and the closing quote


So Real Perl People, what is a better way to do this?

[Disclaimer, the first time I did this I left out the join ...] but that's what 'u' is for in vi.]


And as a follow up I attempted to (vi'ism here) visually select the text to sort and then

:'<,'>!perl -ne '(@w)=split; print join " ", sort @w;'

Which would cause:
  SERVICES="mysql postgresql bind9 jabber nfs-kernel-server nfs-user-server samba apache ssh ntp inetd exim sendmail"  

to become:
  SERVICES="mysql apache bind9 exim inetd jabber nfs-kernel-server nfs-user-server ntp postgresql samba sendmail" ssh 

What?  The first element isn't sorted and (the huge puzzler) the last
element is somehow transmuted to outside the " marks, which is outside the
selected range.  ???  And yes, I repeated the operation while making sure
the " was not part of the range.  I'm assuming that vi is (somehow/why)
including the full non-white space region to the end.

And insight to what's going on with this?

-- 
    Michael Rasmussen  aka  mikeraz
   Be appropriate && Follow your curiosity
   "They that give up essential liberty to obtain
   temporary safety, deserve neither liberty nor safety."
				-- Benjamin Franklin 	
     But keep in mind:
I call them as I see them.  If I can't see them, I make them up.
		-- Biff Barf
TIMTOWTDI



More information about the Pdx-pm-list mailing list