Re: Ordering string of commands in a file
- From: Dave Weaver <zen13097@xxxxxxxxx>
- Date: 30 Dec 2005 10:06:31 GMT
Raghuramaiah Gompa <rgompa@xxxxxxxxxxxxxxxxxxxxx> 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
.
- Follow-Ups:
- Re: Ordering string of commands in a file
- From: Raghuramaiah Gompa
- Re: Ordering string of commands in a file
- References:
- Ordering string of commands in a file
- From: Raghuramaiah Gompa
- Ordering string of commands in a file
- Prev by Date: Re: how to convert the characters ASCII(0-255) to ASCII(0-127)
- Next by Date: Re: how to convert the characters ASCII(0-255) to ASCII(0-127)
- Previous by thread: Re: Ordering string of commands in a file
- Next by thread: Re: Ordering string of commands in a file
- Index(es):
Relevant Pages
|