Connecting Tech Pros Worldwide Help | Site Map

Sorting alphanumber and replace by same order using PERL

Member
 
Join Date: Dec 2007
Posts: 40
#1: Dec 20 '07
Hi.,

I am having the index pg no. data in file .. i need to sort the data ., Can any one help regarding this using PERL .. Its very urgent ...

Source file
<ce:intra-ref id="10011#f0070"/>310f, <ce:intra-ref id="10011#s0120"/>308--309
<ce:intra-ref id="10011#f0130"/>313f, <ce:intra-ref id="10011#p0310"/>313
<ce:intra-ref id="10010#p0520"/>288, <ce:intra-ref id="10010#p0550"/>284--286
<ce:intra-ref id="10007#f0010"/>146f, <ce:intra-ref id="10007#p0030"/>145--146, <ce:intra-ref id="10007#t0010"/>158t

After sorting ...
<ce:intra-ref id="10011#s0120"/>308--309, <ce:intra-ref id="10011#f0070"/>310f
<ce:intra-ref id="10011#p0310"/>313, <ce:intra-ref id="10011#f0130"/>313f
<ce:intra-ref id="10010#p0550"/>284--286, <ce:intra-ref id="10010#p0520"/>284
<ce:intra-ref id="10007#p0030"/>145--146, <ce:intra-ref id="10007#f0010"/>146f, <ce:intra-ref id="10007#t0010"/>158t

Thanks,
Sabarish
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#2: Dec 20 '07

re: Sorting alphanumber and replace by same order using PERL


What have you tried in your efforts to sort?

--Kevin
Member
 
Join Date: Dec 2007
Posts: 40
#3: Dec 22 '07

re: Sorting alphanumber and replace by same order using PERL


Hi Kevin.,

For each line, I split the array using comma [ , ] and stored it in another array @source.
I used the substr function to capture the number to sort.
Perform sorting using alphanumber function which I get in net..
Expand|Select|Wrap|Line Numbers
  1.  
  2. for($i=0; $i<=$#forsort; $i++)
  3. {
  4. for($j=0; $j<=$#sorted; $j++)
  5. {
  6.     if ($forsort[$i] eq $sorted[$j])
  7.     {
  8.         $newarray[$j] = $source[$i] ;
  9.     }
  10. }
  11. }
  12.  
  13. #Function to sort
  14. sub alphanum 
  15. {
  16.     @a = func1($_[0]);
  17.     @b = func1($_[1]);
  18.     while (@a && @b) 
  19.     {
  20.     $a_chunk = shift @a;
  21.     $b_chunk = shift @b;
  22.         $test = (($a_chunk =~ /\d/) && ($b_chunk =~ /\d/)) ? $a_chunk <=> $b_chunk : $a_chunk cmp $b_chunk ;
  23.     return $test if $test != 0;
  24.     }
  25.     return @a <=> @b;
  26. }
  27. sub func1 
  28. {
  29.     @chunks = split m{ (?= (?<=\D)\d | (?<=\d)\D ) }x, $_[0];
  30.       return @chunks;
  31.  
Now using this code is working fine...
but I am facing one more problem...

after sorting if the data is look like means..
<ce:intra-ref id="10009#s0140"/>234, <ce:intra-ref id="10009#s0150"/>234--255, <ce:intra-ref id="10009#s0134"/>274
<ce:intra-ref id="10009#s0240"/>135, <ce:intra-ref id="10009#s0250"/>255, <ce:intra-ref id="10009#s0234"/>255-270

I have to get output like thi
<ce:intra-ref id="10009#s0140"/><ce:intra-ref id="10009#s0150"/>234--255, <ce:intra-ref id="10009#s0134"/>274
<ce:intra-ref id="10009#s0240"/>135, <ce:intra-ref id="10009#s0250"/><ce:intra-ref id="10009#s0234"/>255-270

Looking any help reg this...
Thanks,
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,567
#4: Dec 22 '07

re: Sorting alphanumber and replace by same order using PERL


cnsbar,

Your code tags were almost correct. There should be no spaces when you put "code=perl". Once I removed the spaces, you can now see the results.

Thanks for putting them in.

Happy Holidays!

Jeff
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#5: Dec 22 '07

re: Sorting alphanumber and replace by same order using PERL


Sabarish,

You say you are sorting data, but I can not see what you are sorting. In perl, sorting means to put the data in some type of order, like:

1 2 3 4 5 (numeric ascending)

or

E D C B A (alpha descending)

can you explain specifically what it is you are sorting your data with? Use your original data as your input:

<ce:intra-ref id="10011#f0070"/>310f, <ce:intra-ref id="10011#s0120"/>308--309
<ce:intra-ref id="10011#f0130"/>313f, <ce:intra-ref id="10011#p0310"/>313
<ce:intra-ref id="10010#p0520"/>288, <ce:intra-ref id="10010#p0550"/>284--286
<ce:intra-ref id="10007#f0010"/>146f, <ce:intra-ref id="10007#p0030"/>145--146, <ce:intra-ref id="10007#t0010"/>158t

what part of the above lines are you using to sort the data, and it what order are you sorting it?
Member
 
Join Date: Dec 2007
Posts: 40
#6: Dec 24 '07

re: Sorting alphanumber and replace by same order using PERL


Hi Kevin,

Here I am doing alphanumeric sorting.,
For this I first extract the pageno (310f, 308--309) and perform sorting on this alpha numerals with ascending order.
and then replace with respect to the corresponding tag element <ce:intra-ref id.../>

For example: -
if the i/p line contains..
<start> Some text, <ce:intra-ref id="10007#f0070"/>175, <ce:intra-ref id="10007#p0030"/>145--146, <ce:intra-ref id="10007#t0010"/>158t, <ce:intra-ref id="10007#t0010"/>100</end>

Then after sorting, output to be..
<start> Some text, <ce:intra-ref id="10007#t0010"/>100, <ce:intra-ref id="10007#p0030"/>145--146, <ce:intra-ref id="10007#t0010"/>158t, <ce:intra-ref id="10007#f0070"/>175</end>

I got the desired output also after trying so much of time.
I used simple logic only but for me it takes upto around 100 line to finish that program.
Reply