Sorting a string ?

  • Thread starter Svend-Erik Kjær Madsen
  • Start date
S

Svend-Erik Kjær Madsen

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
 
A

Axel Friedrich

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
 
S

Svend-Erik Kjær Madsen

Axel said:
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!(/\"/,'"')
file << oldfile
file << "<A HREF="?show=#{fil}">#{fil}</A><BR>\n"
file = file.sort
newfile.puts "#{file}"
newfile.close
end

/Best Regards
/Forever RubyNewBee - Svend-E
 
M

Martin DeMello

Svend-Erik Kjær Madsen said:
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
 
S

Svend-Erik Kjær Madsen

Martin DeMello wrote:

Hi
Seems that:

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

can help me :)

Thanks

/Best Regards
/Forever RubyNewBee - Svend-E
 
M

Martin DeMello

Svend-Erik Kjær Madsen said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top