Simple string operation

seqNo =
File.basename("/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV",".*")[-1,3].to_s
seqNo = “-” + seqNo+ “.xx.m”
puts "seqNo: #{seqNo} " # >> seqNo: -4.xx.m

but I want it to be -004.xx.m. In other words I want the leading zeros.

I didn’t even think the .to_s should be necessary, but tried it in
desperation.

Thanks

Newbie (but you already figured that out by the question).

On 7/9/07, 12 34 [email protected] wrote:

seqNo =
File.basename(“/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”,“.*”)[-1,3].to_s
seqNo = “-” + seqNo+ “.xx.m”
puts "seqNo: #{seqNo} " # >> seqNo: -4.xx.m

but I want it to be -004.xx.m. In other words I want the leading zeros.

Maybe you want [-3,3] instead of [-1,3]?

Todd

Todd B. wrote:

On 7/9/07, 12 34 [email protected] wrote:

seqNo =
File.basename(“/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”,“.*”)[-1,3].to_s
seqNo = “-” + seqNo+ “.xx.m”
puts "seqNo: #{seqNo} " # >> seqNo: -4.xx.m

but I want it to be -004.xx.m. In other words I want the leading zeros.

Maybe you want [-3,3] instead of [-1,3]?

Maybe I do.

Thank you

On 10 Jul 2007, at 11:54, 12 34 wrote:

desperation.

Thanks

Your use of String#[] is wrong:

-------------------------------------------------------------- String#[]
Element Reference—If passed a single +Fixnum+, returns the code
of the character at that position. If passed two +Fixnum+ objects,
returns a substring starting at the offset given by the first, and
a length given by the second.

So [-1,3] gives you a string starting at the last character that is 3
characters long. You run off the end of the string before it gets to
3 characters, so it ends up just being the last character - 4. I
wonder if Ruby shouldn’t raise an IndexError in that case.

Replace it with [-4,4] to get what you want (I think) - the last 4
characters. As you mention, the .to_s is unnecessary.

seqNo = File.basename("/Volumes/MINOLTA1/DCIM/100MLT16/
PICT0004.MOV",".*")[-4,4]
seqNo = “-#{seqNo}.xx.m”

Alex G.

Bioinformatics Center
Kyoto University

2007/7/10, Todd B. [email protected]:

On 7/9/07, 12 34 [email protected] wrote:

seqNo =
File.basename(“/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”,“.*”)[-1,3].to_s
seqNo = “-” + seqNo+ “.xx.m”
puts "seqNo: #{seqNo} " # >> seqNo: -4.xx.m

but I want it to be -004.xx.m. In other words I want the leading zeros.

Maybe you want [-3,3] instead of [-1,3]?

Or

seqNo = File.basename(fname)[/\d+/]

cheers

robert

On Jul 9, 8:54 pm, 12 34 [email protected] wrote:

seqNo =
File.basename(“/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”,“.*”)[-1,3].to _s
seqNo = “-” + seqNo+ “.xx.m”
puts "seqNo: #{seqNo} " # >> seqNo: -4.xx.m

but I want it to be -004.xx.m. In other words I want the leading zeros.

Slim2:~ phrogz$ irb
irb(main):001:0> path = “/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”
=> “/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”
irb(main):002:0> digits = path[ /(\d+).\w+$/, 1 ]
=> “0004”
irb(main):003:0> my_seq = “%03d” % digits.to_i
=> “004”

12 34 [email protected] wrote:

seqNo =
File.basename(“/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”,“.*”)[-1,3].to_s
seqNo = “-” + seqNo+ “.xx.m”
puts "seqNo: #{seqNo} " # >> seqNo: -4.xx.m

but I want it to be -004.xx.m. In other words I want the leading zeros.

seqNo =
File.basename(“/Volumes/MINOLTA1/DCIM/100MLT16/PICT0004.MOV”,".")
seqNo = “-” + /\d
$/.match(seqNo)[0] + “.xx.m”
puts "seqNo: #{seqNo} "

But you might need to change that if you don’t want all three leading
zeros (your desired answer omits one). m.