Professional Web Applications Themes

Appending to an array - Ruby

Hi All, I am having a rather weird problem with array manipulation. I have a file with a load of text in it, and I have a line which looks like: vthread << File.new(pathname, "r").readlines().to_s().grep(/^foobar/).uniq() the expression /^foobar/ is repeated throughout, but there are differences. Now, I would assume there to be a new entry added to the array each time grep returns a match. However, while the code does what I want it to, doing a: vthread.length() returns "1" Printing this single element to the screen does indeed yield the results, but what I want it to do is ...

  1. #1

    Default Appending to an array

    Hi All,

    I am having a rather weird problem with array manipulation.

    I have a file with a load of text in it, and I have a line which looks
    like:

    vthread << File.new(pathname,
    "r").readlines().to_s().grep(/^foobar/).uniq()

    the expression /^foobar/ is repeated throughout, but there are
    differences. Now, I would assume there to be a new entry added to the
    array each time grep returns a match. However, while the code does what I
    want it to, doing a:

    vthread.length()

    returns "1"

    Printing this single element to the screen does indeed yield the results,
    but what I want it to do is to add the match each time into a new element
    in the array.

    Am I doing something silly?

    Thank You All,

    Thomas Adam

    =====
    "The Linux Weekend Mechanic" -- http://linuxgazette.net
    "TAG Editor" -- http://linuxgazette.net

    __________________________________________________ ______________________
    BT Yahoo! Broadband - Save 80 when you order online today. Hurry! Offer ends 21st December 2003. The way the internet was meant to be. http://uk.rd./evt=21064/*http://btyahoo.yahoo.co.uk


    Thomas Guest

  2. #2

    Default Re: Appending to an array

    Thomas Adam wrote:
     

    The << operator for array only appends the single object given to the
    end of the array--it does not concatenate one array onto another. What
    you probably want is the += operator:

    vthread += File.new(...).grep(/^foobar/).uniq()

    (Assuming, of course, that vthread is an array.)

    --
    Jamis Buck
    byu.edu

    ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'



    Jamis Guest

  3. #3

    Default Re: Appending to an array

    --- Jamis Buck <byu.edu> wrote:
     
    >
    > The << operator for array only appends the single object given to the
    > end of the array--it does not concatenate one array onto another. What
    > you probably want is the += operator:
    >
    > vthread += File.new(...).grep(/^foobar/).uniq()
    >
    > (Assuming, of course, that vthread is an array.)[/ref]

    James,

    Many thanks for that!! True genius. Solved it completely. I wonder why I
    never thought of it myself :)

    kudos_Points++;

    :)

    -- Thomas Adam

    =====
    "The Linux Weekend Mechanic" -- http://linuxgazette.net
    "TAG Editor" -- http://linuxgazette.net

    __________________________________________________ ______________________
    BT Yahoo! Broadband - Save 80 when you order online today. Hurry! Offer ends 21st December 2003. The way the internet was meant to be. http://uk.rd./evt=21064/*http://btyahoo.yahoo.co.uk

    Thomas Guest

  4. #4

    Default Re: Appending to an array


    The "p" method is awefully handy:

    a = []
    b = []
    c = [1,2,3]
    a << c
    b += c
    p a # => [[1, 2, 3]]
    p b # => [1, 2, 3]

    "puts a" and "puts b" both display the same thing:
    1
    2
    3

    Not very helpful :)


    On Fri, 12 Dec 2003, Thomas Adam wrote:
     
    > >
    > > The << operator for array only appends the single object given to the
    > > end of the array--it does not concatenate one array onto another. What
    > > you probably want is the += operator:
    > >
    > > vthread += File.new(...).grep(/^foobar/).uniq()
    > >
    > > (Assuming, of course, that vthread is an array.)[/ref]
    >
    > James,
    >
    > Many thanks for that!! True genius. Solved it completely. I wonder why I
    > never thought of it myself :)
    >
    > kudos_Points++;
    >
    > :)
    >
    > -- Thomas Adam
    >
    > =====
    > "The Linux Weekend Mechanic" -- http://linuxgazette.net
    > "TAG Editor" -- http://linuxgazette.net
    >
    > __________________________________________________ ______________________
    > BT Yahoo! Broadband - Save 80 when you order online today. Hurry! Offer ends 21st December 2003. The way the internet was meant to be. http://uk.rd./evt=21064/*http://btyahoo.yahoo.co.uk
    >[/ref]

    Derek Lewis

    ================================================== =================
    Java Web-Application Developer

    Email : com
    Cellular : 604.312.2846
    Website : http://www.lewisd.com

    "If you've got a 5000-line JSP page that has "all in one" support
    for three input forms and four follow-up screens, all controlled
    by "if" statements in scriptlets, well ... please don't show it
    to me :-). Its almost dinner time, and I don't want to lose my
    appetite :-)."
    - Craig R. McClanahan


    Derek Guest

  5. #5

    Default Re: Appending to an array


    On Dec 11, 2003, at 14:46, Thomas Adam wrote:
     

    Here uniq() returns an array and you are adding this single object of
    class Array into vthread. You may want to use

    vthread.concat File.new ...

    instead.

    By the way, your example leaves an opened file behind until it is
    garbage collected. While possibly being OK in your environment, in
    general it is better not to leave opened files when you can easily have
    them closed. You can use:

    IO.readlines(pathname) instead of File.new(pathname,"r").readlines or
    do this

    vthread.concat File.open(pathname,"r") { |_io|
    _io.readlines().to_s().grep(/^foobar/).uniq()
    }

    When block is done, file will be closed.
     

    Sincerely,
    Gennady Bystritsky



    Gennady Guest

  6. #6

    Default Re: Appending to an array


    "Jamis Buck" <byu.edu> schrieb im Newsbeitrag
    news:byu.edu... 
    >
    > The << operator for array only appends the single object given to the
    > end of the array--it does not concatenate one array onto another. What
    > you probably want is the += operator:
    >
    > vthread += File.new(...).grep(/^foobar/).uniq()[/ref]

    Not good: this creates new arrays all the time. Also: File.readlines is
    better since it closes the file automatically:

    vthread.push *File.readlines(pathname).grep(/^foobar/).uniq()
    or
    vthread.concat File.readlines(pathname).grep(/^foobar/).uniq()

    Kind regards

    robert

    Robert Guest

Similar Threads

  1. Appending to a string?
    By JakeFlynn in forum Macromedia ColdFusion
    Replies: 6
    Last Post: June 2nd, 01:24 PM
  2. Appending at various locations
    By MJS in forum PERL Beginners
    Replies: 0
    Last Post: September 7th, 05:15 AM
  3. += vs << when appending arrays to an array
    By Doug Beaver in forum Ruby
    Replies: 1
    Last Post: July 16th, 08:57 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139