Professional Web Applications Themes

(noob) cast string to array? - Ruby

Hi. What a fantastic language. This is my first day learning using it and I *love* it so far. I am not very comfortable with Ruby speak yet, so I hope the explaination of my problem makes some kind of sense. I have a program that fetches some results from a shell script. The result is a string, not a block as it may look like below. result = { "foobar", "foobar", "foobar" } I want to basically remove the {}'s and replace them with [] and cast result as an array so that I can have some fun within ...

  1. #1

    Default (noob) cast string to array?


    Hi. What a fantastic language. This is my first day learning using it
    and I *love* it so far.

    I am not very comfortable with Ruby speak yet, so I hope the
    explaination of my problem makes some kind of sense.

    I have a program that fetches some results from a shell script. The
    result is a string, not a block as it may look like below.

    result = { "foobar", "foobar", "foobar" }

    I want to basically remove the {}'s and replace them with [] and cast
    result as an array so that I can have some fun within Ruby.

    I figured something like this:

    result = result.sub(/\{/,'[')
    result = result.sub(/\}/,']')

    Now I have a string that *LOOKS* like and Array but isn't....

    Any suggestions are more than welcome.

    --
    Koncept <<
    "Contrary to popular belief, the most dangerous animal is not the lion or
    tiger or even the elephant. The most dangerous animal is a shark riding
    on an elephant, just ling and eating everything they see." - Jack Handey
    Koncept Guest

  2. #2

    Default Re: (noob) cast string to array?

    On Thu, 12 Feb 2004 00:31:29 +0000, Koncept wrote: 

    Great.. Welcome to Ruby.

    [snip] 

    What you has written is the same as (notice the '!' suffix)

    result.sub!(/\{/,'[')
    result.sub!(/\}/,']')

    Above can be chained, so it becomes

    result.sub!(/\{/,'[').sub!(/\}/,']')
     

    Try 'gsub'

    result.gsub!(/\{/,'[').gsub!(/\}/,']')


    --
    Simon Strandgaard
    Simon Guest

  3. #3

    Default Re: (noob) cast string to array?

    On Thursday 12 February 2004 00:35, Koncept wrote: 

    irb(main):002:0> s = '["a string", "another string"]'
    => "[\"a string\", \"another string\"]"
    irb(main):003:0> array = eval s
    => ["a string", "another string"]
    irb(main):004:0> array.class
    => Array


    Martin Hart
    Arnclan Limited
    53 Union Street
    Dunstable, Beds
    LU6 1EX
    http://www.arnclanit.com




    Martin Guest

  4. #4

    Default Re: (noob) cast string to array?

    # sub { block way! }

    result.sub!(/\{(.+)\}/) { "["+$1+"]" }

    -ronnie.


    Simon Strandgaard wrote:
     
    >
    >Great.. Welcome to Ruby.
    >
    >[snip]
    >

    >
    >What you has written is the same as (notice the '!' suffix)
    >
    > result.sub!(/\{/,'[')
    > result.sub!(/\}/,']')
    >
    >Above can be chained, so it becomes
    >
    > result.sub!(/\{/,'[').sub!(/\}/,']')
    >
    >

    >
    >Try 'gsub'
    >
    > result.gsub!(/\{/,'[').gsub!(/\}/,']')
    >
    >
    >--
    >Simon Strandgaard
    >
    >[/ref]




    Bermejo, Guest

  5. #5

    Default Re: (noob) cast string to array?

    In article <com>, Martin Hart
    <com> wrote:
     

    This is great! Thanks Martin. Thanks for all the fast responses as well
    everybody.

    I just wanted to follow up on some of the responses here:

    1) Suggestion to use gsub over sub. I assume that gsub is global?
    2) what is the "!" responsible for doing? (Remember me = n00b )
     

    --
    Koncept <<
    "Contrary to popular belief, the most dangerous animal is not the lion or
    tiger or even the elephant. The most dangerous animal is a shark riding
    on an elephant, just ling and eating everything they see." - Jack Handey
    Koncept Guest

  6. #6

    Default Re: (noob) cast string to array?

    On Thursday 12 February 2004 01:34, David Naseby wrote: 
    >
    > From: Koncept [mailto:invalid]

    > >
    > >This is great! Thanks Martin. Thanks for all the fast responses as well
    > >everybody.[/ref]
    >
    > Avoid eval!!![/ref]

    I think that may be a bit harsh :-)
    As long as one is aware of when and when not to use it (the same as
    everything else) then it has a useful and valid purpose.

    Cheers,
    Martin





    Martin Guest

  7. #7

    Default Re: (noob) cast string to array?

    In article <110220042027215889%invalid>, Koncept
    <invalid> wrote:
     

    Ignore this please. It was answered. Nice timesaver....

    --
    Koncept <<
    "Contrary to popular belief, the most dangerous animal is not the lion or
    tiger or even the elephant. The most dangerous animal is a shark riding
    on an elephant, just ling and eating everything they see." - Jack Handey
    Koncept Guest

  8. #8

    Default Re: (noob) cast string to array?

    In article
    <XOCWb.33248$bloor.is.net.cable.rogers.com>, Mike
    Stok <co.uk> wrote:
     

    The {} is the result of issuing a command to AppleScript. This is the
    way Applescript formats "Lists" or Arrays.
     [/ref]
    > => "{ \"robert\", \"trey\", \"adrian\", \"pat\" }" [/ref]
    > => ["robert", "trey", "adrian", "pat"]
    >[/ref]

    Forgive me if I am missing something...

    irb(main):001:0> result = '{ "robert","trey","adrian", "pat" }'
    => "{ \"robert\",\"trey\",\"adrian\", \"pat\" }"

    irb(main):002:0> result.scan(/"(.*?)"/).flatten
    => ["robert", "trey", "adrian", "pat"]

    irb(main):004:0> result.class
    => *String*

    irb(main):005:0> result = ["robert","trey","adrian","pat"]
    => ["robert", "trey", "adrian", "pat"]

    irb(main):006:0> result.class
    => *Array*

    Your solution formatted my string to look like an Array, but the result
    still has a class of String. :(

    How can I cast this String as an Array?

     

    --
    Koncept <<
    "Contrary to popular belief, the most dangerous animal is not the lion or
    tiger or even the elephant. The most dangerous animal is a shark riding
    on an elephant, just ling and eating everything they see." - Jack Handey
    Koncept Guest

  9. #9

    Default Re: (noob) cast string to array?


    "Koncept" <invalid> schrieb im Newsbeitrag
    news:120220041122469448%invalid... [/ref]
    the 
    >
    > The {} is the result of issuing a command to AppleScript. This is the
    > way Applescript formats "Lists" or Arrays.
    > [/ref]
    experiment: 
    > > => "{ \"robert\", \"trey\", \"adrian\", \"pat\" }" 
    > > => ["robert", "trey", "adrian", "pat"]
    > >[/ref]
    >
    > Forgive me if I am missing something...
    >
    > irb(main):001:0> result = '{ "robert","trey","adrian", "pat" }'
    > => "{ \"robert\",\"trey\",\"adrian\", \"pat\" }"
    >
    > irb(main):002:0> result.scan(/"(.*?)"/).flatten
    > => ["robert", "trey", "adrian", "pat"]
    >
    > irb(main):004:0> result.class
    > => *String*
    >
    > irb(main):005:0> result = ["robert","trey","adrian","pat"]
    > => ["robert", "trey", "adrian", "pat"]
    >
    > irb(main):006:0> result.class
    > => *Array*
    >
    > Your solution formatted my string to look like an Array, but the result
    > still has a class of String. :(
    >
    > How can I cast this String as an Array?[/ref]

    What do you think is the type of the result of line 002?

    robert

     
    >
    > --
    > Koncept <<
    > "Contrary to popular belief, the most dangerous animal is not the lion[/ref]
    or 
    Handey

    Robert Guest

  10. #10

    Default Re: (noob) cast string to array?

    On Thu, 12 Feb 2004 17:35:20 +0100, Robert Klemme wrote: [/ref]


    Try

    result = result.scan(/"(.*?)"/).flatten

     [/ref]


    --
    Simon Strandgaard
    Simon Guest

  11. #11

    Default Re: (noob) cast string to array?

    In article <120220041122469448%invalid>,
    Koncept <invalid> wrote: 
    >
    >The {} is the result of issuing a command to AppleScript. This is the
    >way Applescript formats "Lists" or Arrays.

    >> => "{ \"robert\", \"trey\", \"adrian\", \"pat\" }" 
    >> => ["robert", "trey", "adrian", "pat"]
    >>[/ref]
    >
    >Forgive me if I am missing something...
    >
    >irb(main):001:0> result = '{ "robert","trey","adrian", "pat" }'
    >=> "{ \"robert\",\"trey\",\"adrian\", \"pat\" }"
    >
    >irb(main):002:0> result.scan(/"(.*?)"/).flatten
    >=> ["robert", "trey", "adrian", "pat"]
    >
    >irb(main):004:0> result.class
    >=> *String*
    >
    >irb(main):005:0> result = ["robert","trey","adrian","pat"]
    >=> ["robert", "trey", "adrian", "pat"]
    >
    >irb(main):006:0> result.class
    >=> *Array*
    >
    >Your solution formatted my string to look like an Array, but the result
    >still has a class of String. :([/ref]

    irb was just showing what the value of the expression was. If you want
    to apply the technique then either store the result
     [/ref]
    => "{ \"robert\",\"trey\",\"adrian\", \"pat\" }" [/ref]
    => ["robert", "trey", "adrian", "pat"] [/ref]
    => String [/ref]
    => Array

    or use it immediately e.g.
     [/ref]
    ?> puts "got >#{name}<" [/ref]
    got >robert<
    got >trey<
    got >adrian<
    got >pat<
    => ["robert", "trey", "adrian", "pat"]

    In general ruby methods don't change object being operated upon unless
    they have a "destructive sounding name" (e.g. Array#delete) or end with
    a ! (e.g. String#gsub!)

    Hope this helps,

    Mike


    --
    co.uk | The "`Stok' disclaimers" apply.
    http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
    com | Fingerprint 0570 71CD 6790 7C28 3D60
    http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA
    Mike Guest

  12. #12

    Default Re: (noob) cast string to array?

    On Thursday 12 February 2004 16:24, Koncept wrote: 

    I think that you are discarding the result of flatten....

    (not tested)
    array = result.scan(...).flatten
    array.class

    Cheers,
    Martin




    Martin Guest

  13. #13

    Default Re: (noob) cast string to array?

    In article <com>, Martin Hart
    <com> wrote:
     

    That is exactly what beginners are good at doing. You are absolutely
    correct.

    Mike's solution worked perfect. It was me that was failing. DOH!

    --
    Koncept <<
    "Contrary to popular belief, the most dangerous animal is not the lion or
    tiger or even the elephant. The most dangerous animal is a shark riding
    on an elephant, just ling and eating everything they see." - Jack Handey
    Koncept Guest

  14. #14

    Default Re: (noob) cast string to array?


    "Simon Strandgaard" <dk> schrieb im Newsbeitrag
    news:dk... [/ref]
    >
    >
    > Try
    >
    > result = result.scan(/"(.*?)"/).flatten
    >
    > [/ref][/ref]

    Dunno what you did, but scan returns an array:

    irb(main):004:0> result = '{ "robert","trey","adrian", "pat" }'
    => "{ \"robert\",\"trey\",\"adrian\", \"pat\" }"
    irb(main):005:0> result.scan(/"(.*?)"/).flatten
    => ["robert", "trey", "adrian", "pat"]
    irb(main):006:0> result.scan(/"(.*?)"/).flatten.class
    => Array
    irb(main):007:0> result = result.scan(/"(.*?)"/).flatten
    => ["robert", "trey", "adrian", "pat"]
    irb(main):008:0> result.class
    => Array
    irb(main):009:0>

    robert

    Robert Guest

Similar Threads

  1. Web Service Help for a Noob : Returning an XML String
    By Nastero in forum ASP.NET Web Services
    Replies: 2
    Last Post: March 31st, 04:13 PM
  2. [PHP-DEV] SimpleXML and Default Cast To String
    By Blake in forum PHP Development
    Replies: 37
    Last Post: January 14th, 04:43 PM
  3. Replies: 2
    Last Post: August 16th, 10:26 AM
  4. Cast from String To Date
    By S. Justin Gengo in forum ASP.NET General
    Replies: 0
    Last Post: July 16th, 05:02 PM

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