[26718] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8813 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Dec 31 14:05:30 2005

Date: Sat, 31 Dec 2005 11:05:03 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 31 Dec 2005     Volume: 10 Number: 8813

Today's topics:
    Re: Ordering string of commands in a file (Raghuramaiah Gompa)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 31 Dec 2005 16:29:07 +0000 (UTC)
From: rgompa@steel.ucs.indiana.edu (Raghuramaiah Gompa)
Subject: Re: Ordering string of commands in a file
Message-Id: <dp6bkj$s5q$1@rainier.uits.indiana.edu>

In article <43b506a6$0$898$db0fefd9@news.zen.co.uk>,
Dave Weaver  <zen13097@zen.co.uk> wrote:
>Raghuramaiah Gompa <rgompa@steel.ucs.indiana.edu> wrote:
>>  I have a file that has lines like the following:
>>  
>>  	Titl1=B2a
>>  	Command1=c:\chi\cw.exe
>>  	Title2=f2a
>>  	Directory1=c:\chi
>>  	Type1=1
>>  	Command2=c:\chi\cw.exe
>>  	Directory2=c:\chi
>>  	Options2=%@%
>>  	Type11=2
>>  	Title14=Ma3
>>  	Command14=c:\chi\cpb.bat
>>  	Options14=c:\temp\temp.bin
>>  	Use long names3=1
>>  	Title12=Dir
>>  	Command12=Tfd
>>  	Title13=a2h
>>  	Command13=c:\bat\a2h.bat 
>>  	Options13=%@dpn% 
>>  	Show13=2
>>  	Command31=C:\bat\tview.bat
>>  	Use long names31=1
>>  	Title31=tview 
>>   	.....
>>  
>>  Basically Title,Command, Show,... constitute a group
>of commands
>>  that are associated with the name described in
>Title.  Now I want
>>  to reorder them in the alphabetical order of names
>described in
>>  Title.  For instance, the new file should have :
>>  
>>  	Title1=a2h	Command1=c:\bat\a2h.bat Options1=%@dpn%
>>  	Show1=2
>>  	
>>  and all the lines (Title2=B2a, ..) are changed
>accordingly (because
>>  or the order a2h, B2a, Dir, f2a, Ma3, .... )
>
>Your description is not very clear.
>
>You want to group the lines where the number is in
>common (so Title14
>goes with Options14 and Command14, and so forth)?
>
>Then sort the groups them based on the value of the
>Title element?
>What about when there is no Title elelement, such as in Type11
>above?
>
>Then ouput in this new order, but renumber them, so the
>the first
>group is always Title1=... , then second has Title2=...
>and so on?
>
>Is that what you mean?
>
>The code below *might* do something like you want, but
>since your
>specification is lacking, the results may be too.
>
>Interesting (to me) sidenote: I noticed that when I
>added the lc()
>calls to the sort, the warnings about use of undefined
>variables
>(because Title isn't defined for some elements) went
>away. Shouldn't
>lc() warn if it's passed undef?
>
>
>#!/usr/bin/perl
>use warnings;
>use strict;
>
>my %items;
>while(<DATA>) {
>    $items{$2}{$1} = $3 if /^(\w+?)(\d+)=(.+)/;
>}
>
>my $n = 1;
>for my $idx (
>     sort {
>	    lc $items{$a}{Title} cmp lc $items{$b}{Title}
>	} keys %items)
>{
>    print map {
>		"$_$n=$items{$idx}{$_}\n";
>	    } keys %{$items{$idx}};
>    print "----\n";
>    ++$n;
>}
>
>__DATA__
>Title1=B2a
>Command1=c:\chi\cw.exe
>Title2=f2a
>Directory1=c:\chi
>Type1=1
>Command2=c:\chi\cw.exe
>Directory2=c:\chi
>Options2=%@%
>Type11=2
>Title14=Ma3
>Command14=c:\chi\cpb.bat
>Options14=c:\temp\temp.bin
>Use long names3=1
>Title12=Dir
>Command12=Tfd
>Title13=a2h
>Command13=c:\bat\a2h.bat
>options13=%@dpn%
>Show13=2
>Command31=C:\bat\tview.bat
>Use long names31=1
>Title31=tview
>

First of all, I apologize for not making my question 
clear.  However, you (and others, too) understood exactly 
what I want.  Thank you all for the codes and help. Now I 
am faced with another problem.  This sorting must be done 
only for a part of the text that is between the 
line that contain [Buttons] and someother line that 
starts with [ (it could be [Directories] or [Files]).  
Rest of the document should be copied as it is.  I tried 
several ways and failed :(  Please help.  .. Raghu  


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 8813
***************************************


home help back first fref pref prev next nref lref last post