Sorting a string ?

Discussion in 'Ruby' started by Svend-Erik Kjær Madsen, Mar 2, 2005.

  1. 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
    Svend-Erik Kjær Madsen, Mar 2, 2005
    #1
    1. Advertising

  2. Svend-Erik Kjær Madsen <> wrote in
    news:4225855c$0$29437$:

    > 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
    Axel Friedrich, Mar 2, 2005
    #2
    1. Advertising

  3. 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
    Svend-Erik Kjær Madsen, Mar 2, 2005
    #3
  4. Svend-Erik Kjær Madsen <> 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
    Martin DeMello, Mar 3, 2005
    #4
  5. Martin DeMello wrote:

    Hi
    Seems that:

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

    can help me :)

    Thanks

    /Best Regards
    /Forever RubyNewBee - Svend-E
    Svend-Erik Kjær Madsen, Mar 3, 2005
    #5
  6. Svend-Erik Kjær Madsen <> 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
    Martin DeMello, Mar 4, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom St Denis

    Sorting a string aray in order of string length

    Tom St Denis, Jul 9, 2003, in forum: C Programming
    Replies:
    9
    Views:
    473
    Kevin Easton
    Jul 10, 2003
  2. Replies:
    2
    Views:
    1,419
    James Kanze
    Jul 6, 2010
  3. Jason
    Replies:
    0
    Views:
    380
    Jason
    Oct 4, 2006
  4. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    472
    Michael Budash
    Oct 11, 2003
  5. Íéêüëáïò Êïýñáò

    Sorting a set works, sorting a dictionary fails ?

    Íéêüëáïò Êïýñáò, Jun 10, 2013, in forum: Python
    Replies:
    12
    Views:
    155
    Ulrich Eckhardt
    Jun 10, 2013
Loading...

Share This Page