Forum: Ruby Simple string operation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Greg _. (Guest)
on 2007-07-10 06:54
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).
Todd B. (Guest)
on 2007-07-10 07:35
(Received via mailing list)
On 7/9/07, 12 34 <removed_email_address@domain.invalid> 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
Greg _. (Guest)
on 2007-07-10 07:53
Todd B. wrote:
> On 7/9/07, 12 34 <removed_email_address@domain.invalid> 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
Alex G. (Guest)
on 2007-07-10 08:03
(Received via mailing list)
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
Robert K. (Guest)
on 2007-07-10 10:27
(Received via mailing list)
2007/7/10, Todd B. <removed_email_address@domain.invalid>:
> On 7/9/07, 12 34 <removed_email_address@domain.invalid> 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
unknown (Guest)
on 2007-07-11 17:44
(Received via mailing list)
12 34 <removed_email_address@domain.invalid> 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.
Phrogz (Guest)
on 2007-07-11 17:45
(Received via mailing list)
On Jul 9, 8:54 pm, 12 34 <removed_email_address@domain.invalid> 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"
This topic is locked and can not be replied to.