Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Sorting a string ?

Reply
Thread Tools

Sorting a string ?

 
 
Svend-Erik Kjr Madsen
Guest
Posts: n/a
 
      03-02-2005
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
 
Reply With Quote
 
 
 
 
Axel Friedrich
Guest
Posts: n/a
 
      03-02-2005
Svend-Erik Kjr Madsen <(E-Mail Removed)> wrote in
news:4225855c$0$29437$(E-Mail Removed) :

> 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

 
Reply With Quote
 
 
 
 
Svend-Erik Kjr Madsen
Guest
Posts: n/a
 
      03-02-2005
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!(/\"/,'"')
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
 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      03-03-2005
Svend-Erik Kjr Madsen <(E-Mail Removed)> 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
 
Reply With Quote
 
Svend-Erik Kjr Madsen
Guest
Posts: n/a
 
      03-03-2005
Martin DeMello wrote:

Hi
Seems that:

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

can help me

Thanks

/Best Regards
/Forever RubyNewBee - Svend-E

 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      03-04-2005
Svend-Erik Kjr Madsen <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting list vs sorting vector boltar2003@boltar.world C++ 2 07-06-2010 09:40 AM
fired event Sorting which wasn't handled - sorting and SelectedIndexChanged Jason ASP .Net Web Controls 0 10-04-2006 02:19 PM
Help with changing char *string to int * string for sorting. Pokerkook C Programming 4 12-10-2004 01:33 PM
sorting by multiple criterias (sub-sorting) Tom Kirchner Perl Misc 3 10-11-2003 05:16 PM
Sorting a string aray in order of string length Tom St Denis C Programming 9 07-10-2003 10:08 AM



Advertisments