Re: Ordering string of commands in a file



you may need to reset $/ to read your data, and then use hash keys to
sort your results. see the following code.
-----------------------------------------------------------------------
#!/usr/bin/perl -w
use strict;
my %h=();
my $n;
{
local $/="Title";
while(<DATA>) {
chomp;
$h{$1}=$2 if(/\A\d+=(.*?)\n(.*)\z/sm);
}
}
foreach my $k(sort{lc $a cmp lc $b} keys %h) {
$n++;
print "Title$n=$k\n$h{$k}";
}
__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
-----------------------------------------------------------------------
Raghuramaiah Gompa 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, .... )
>
> How can I achieve this? Please help. .. Raghu

.



Relevant Pages

  • Re: Toliet Seat Secrets
    ... For Zack the theme's latin, sort of me it's strict, whereas ...
    (rec.audio.car)
  • Re: p630 hangup
    ... It can be difficult to deal with this sort of problem. ... Usually a reset is ... If it's a memory leak, there are some IBM tools to help diagnose them. ... in some more intrusive monitoring. ...
    (AIX-L)
  • Re: 2 Gigs of RAM...
    ... I only upgraded for the RAW converter. ... decided there was some danger here so hit the reset button my machine. ... I've never had this sort of problem with a program before... ...
    (rec.photo.digital)
  • Re: 2 Gigs of RAM...
    ... I tried the three finger shuffle and at ... > decided there was some danger here so hit the reset button my machine. ... > I've never had this sort of problem with a program before... ...
    (rec.photo.digital)
  • Re: Sorting AofH over hash key(s)...
    ... cages here on c.l.p.m... ... I want to be able to sort ... an Array of Hashes by an arbitrary number of hash keys. ... # is like saying use 'cmp' to compare the first key ...
    (comp.lang.perl.misc)