separate a string of values into an array - noob

Discussion in 'Ruby' started by Mmcolli00 Mom, Jun 16, 2009.

  1. Hi. I am don't understand how to separate my string of filenames into an
    array. For example, arrayDiscrep[0] outputs the whole array when I need
    it to only output the first element at index 0 which would be filename:
    234234. Do you know how I can separate each filename for the array?
    Thanks MC

    I have this directory of filenames.
    234234.EXE 234234.EXE 234545.EXE 34543435.EXE 234563.EXE 24324345.EXE...

    Dir["C:/DiscrepancyFiles/*EXE"].each do |disFile|
    stringDisFile = File.basename(disFile).gsub("EXE,"")

    arrayDiscrep = Array.new
    arrayDiscrep = stringDisFile.to_a
    puts arrayDiscrep[0]
    end
    --
    Posted via http://www.ruby-forum.com/.
     
    Mmcolli00 Mom, Jun 16, 2009
    #1
    1. Advertising

  2. Mmcolli00 Mom wrote:
    > Hi. I am don't understand how to separate my string of filenames into an
    > array. For example, arrayDiscrep[0] outputs the whole array when I need
    > it to only output the first element at index 0 which would be filename:
    > 234234. Do you know how I can separate each filename for the array?
    > Thanks MC
    >
    > I have this directory of filenames.
    > 234234.EXE 234234.EXE 234545.EXE 34543435.EXE 234563.EXE 24324345.EXE...
    >
    > Dir["C:/DiscrepancyFiles/*EXE"].each do |disFile|
    > stringDisFile = File.basename(disFile).gsub("EXE,"")
    >
    > arrayDiscrep = Array.new
    > arrayDiscrep = stringDisFile.to_a
    > puts arrayDiscrep[0]
    > end


    Everything between the "each"and "end" is a loop, so you are making a
    new array for each file.

    File.basename has a nice feature; if you specify a suffix it is removed.

    ar = Dir["C:/DiscrepancyFiles/*EXE"].map do |dis_file|
    File.basename(dis_file, ".exe")
    end
    puts ar[0]

    hth,

    Siep
    --
    Posted via http://www.ruby-forum.com/.
     
    Siep Korteling, Jun 16, 2009
    #2
    1. Advertising

  3. Mmcolli00 Mom, Jun 16, 2009
    #3
  4. Siep
    > ar = Dir["C:/DiscrepancyFiles/*EXE"].map do |dis_file|
    > File.basename(dis_file, ".exe")
    > end
    > puts ar[0]
    >
    > hth,
    >
    > Siep



    What if it weren't coming from a directory could you still you .map?
    -Misty
    --
    Posted via http://www.ruby-forum.com/.
     
    Mmcolli00 Mom, Jun 16, 2009
    #4
  5. Mmcolli00 Mom wrote:
    > What if it weren't coming from a directory could you still you .map?
    > -Misty


    The way to do this depends on what the input is, if not a directory?

    I don't know how you'd get this input, but let's say you have a string:

    input = "234234.EXE 234234.EXE 234545.EXE 34543435.EXE 234563.EXE
    24324345.EXE"

    tokens = input.split(' ')
    first_token = tokens.first
    remove_suffix = first_token.split('.').first

    # Or all in one line if you like:

    input.split(' ').first.split('.').first


    There are a variety of other ways to do 'this', but what 'this' is
    depends on, well, what you're doing.
    --
    Posted via http://www.ruby-forum.com/.
     
    Jonathan Rochkind, Jun 16, 2009
    #5
    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. EvgueniB
    Replies:
    1
    Views:
    663
    Anthony Borla
    Dec 15, 2003
  2. anti-guru
    Replies:
    2
    Views:
    7,848
    anti-guru
    Sep 2, 2004
  3. Frank Fredstone
    Replies:
    1
    Views:
    463
    Jean-Francois Briere
    Jun 27, 2006
  4. Koncept

    (noob) cast string to array?

    Koncept, Feb 12, 2004, in forum: Ruby
    Replies:
    19
    Views:
    234
    Robert Klemme
    Feb 13, 2004
  5. Ben
    Replies:
    4
    Views:
    125
    -berlin.de
    Feb 23, 2007
Loading...

Share This Page