[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Sorting a string ?

Svend-Erik Kjær Madsen

3/2/2005 9:56:00 AM

Hi
I have a file, only containing html links, I read the file in a
variable, an then I want to sort all that differs from htmltags.

My Regexp string is: (?-mix:<("[^"]*"|'[^']*'|[^ '">])*>)

Is it possible to sort the string, without putting it all in an array first?

If so, can anyone give me a hint, how to solve the problem.

/Best Regards
/Forever RubyNewBee - Svend-E
5 Answers

Axel Friedrich

3/2/2005 7:40:00 PM

0

Svend-Erik Kjær Madsen <sv-erik@stofanet.dk> wrote in
news:4225855c$0$29437$ba624c82@nntp06.dk.telia.net:

> Hi
> I have a file, only containing html links, I read the file in a
> variable, an then I want to sort all that differs from htmltags.
>
> My Regexp string is: (?-mix:<("[^"]*"|'[^']*'|[^ '">])*>)
>
> Is it possible to sort the string, without putting it all in an
> array first?

Could You give an example?
Sort which string by what?

Axel

--
axel o friedrich_smail & gmx o de

Svend-Erik Kjær Madsen

3/2/2005 9:31:00 PM

0

Axel Friedrich wrote:
> Could You give an example?
> Sort which string by what?
>
> Axel
>
I want to sort the non htmltag stuff, like in:

<A HREF="index.php">home</A><BR>
<A HREF="?show=test">test</A><BR>
<A HREF="?show=test1">test1</A><BR>

Thats what my oldfile "in my script" contains.


This is what i come up with so far, but my file = file.sort, do not seem
to sort.

#!/usr/bin/env ruby
current_user = ENV["LOGNAME"]
if current_user == "root"
echo "You cannot run this script as root !"
exit 1
end
fil =`Xdialog --stdout --fselect "*" * *`
if fil != ""
fil = File.basename("#{fil}")
fil.chomp!
readfile = File.open(fil)
oldfile = readfile.read
readfile.close
newfile = File.open("#{fil}_n","w")
file = ""
oldfile.gsub!(/\"/,'&#34;')
file << oldfile
file << "<A HREF=&#34;?show=#{fil}&#34;>#{fil}</A><BR>\n"
file = file.sort
newfile.puts "#{file}"
newfile.close
end

/Best Regards
/Forever RubyNewBee - Svend-E

Martin DeMello

3/3/2005 8:01:00 AM

0

Svend-Erik Kjær Madsen <sv-erik@stofanet.dk> wrote:
> Hi
> I have a file, only containing html links, I read the file in a
> variable, an then I want to sort all that differs from htmltags.
>
> My Regexp string is: (?-mix:<("[^"]*"|'[^']*'|[^ '">])*>)
>
> Is it possible to sort the string, without putting it all in an array first?
>
> If so, can anyone give me a hint, how to solve the problem.

Do you mean you want to sort the lines of the file, but only sort based
on the text not in the tags? If so, try (not tested!)

lines = IO.readlines(inputfile)
sorted = lines.sort_by {|i| i.gsub /<.*?>/, ""}

martin

Svend-Erik Kjær Madsen

3/3/2005 9:49:00 AM

0

Martin DeMello wrote:

Hi
Seems that:

sort_by {|i| i.gsub /<.*?>/, ""}

can help me :-)

Thanks

/Best Regards
/Forever RubyNewBee - Svend-E

Martin DeMello

3/4/2005 7:02:00 AM

0

Svend-Erik Kjær Madsen <sv-erik@stofanet.dk> wrote:
> Martin DeMello wrote:
>
> Hi
> Seems that:
>
> sort_by {|i| i.gsub /<.*?>/, ""}
>
> can help me :-)

Cool :) Play with the sort_by function a bit - it's pretty cool.

martin