[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Simple string operation

12 34

7/10/2007 2:54:00 AM

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).

--
Posted via http://www.ruby-....

6 Answers

matt

7/10/2007 3:08:00 AM

0

12 34 <rubyforum@web.knobby.ws> 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.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits...
Tiger - http://www.takecontrolbooks.com/tiger-custom...
AppleScript - http://www.amazon.com/gp/product/...
Read TidBITS! It's free and smart. http://www.t...

Todd Benson

7/10/2007 3:34:00 AM

0

On 7/9/07, 12 34 <rubyforum@web.knobby.ws> 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

12 34

7/10/2007 3:53:00 AM

0

Todd Benson wrote:
> On 7/9/07, 12 34 <rubyforum@web.knobby.ws> 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


--
Posted via http://www.ruby-....

Alex Gutteridge

7/10/2007 4:03:00 AM

0

On 10 Jul 2007, at 11:54, 12 34 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.
>
> I didn't even think the .to_s should be necessary, but tried it in
> 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 Gutteridge

Bioinformatics Center
Kyoto University



Phrogz

7/10/2007 5:35:00 AM

0

On Jul 9, 8:54 pm, 12 34 <rubyfo...@web.knobby.ws> 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"

Robert Klemme

7/10/2007 6:26:00 AM

0

2007/7/10, Todd Benson <caduceass@gmail.com>:
> On 7/9/07, 12 34 <rubyforum@web.knobby.ws> 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