Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > separate a string of values into an array - noob

Reply
Thread Tools

separate a string of values into an array - noob

 
 
Mmcolli00 Mom
Guest
Posts: n/a
 
      06-16-2009
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/.

 
Reply With Quote
 
 
 
 
Siep Korteling
Guest
Posts: n/a
 
      06-16-2009
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/.

 
Reply With Quote
 
 
 
 
Mmcolli00 Mom
Guest
Posts: n/a
 
      06-16-2009
Cool it worked! Thanks Siep!
-MC
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Mmcolli00 Mom
Guest
Posts: n/a
 
      06-16-2009
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/.

 
Reply With Quote
 
Jonathan Rochkind
Guest
Posts: n/a
 
      06-16-2009
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/.

 
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
Here a noob, there a noob.... JimDoire MCSE 0 04-10-2008 07:23 PM
Separate Tabs, Separate Sessions BigAndy Firefox 0 05-09-2007 09:27 AM
Separate Tabs, Separate Sessions BigAndy Firefox 0 05-09-2007 09:26 AM
Using separate classpaths for separate classes? Frank Fredstone Java 1 06-27-2006 06:46 AM
How to use several separate classes (separate files) to be executed in one class (another file) EvgueniB Java 1 12-15-2003 01:18 AM



Advertisments