Substrings by Position

Hi,

Just a logic question about substrings by position.

foo = “012345678”
foo[5,1] #-> “5”
foo[-4,2] #-> “56”
foo[-3,2] = “” #-> “0123458”

Shouldn’t a negative indexing work as well? (Below are just examples,
they currently don’t work) It will allow strings to be REVERSED easily,
and thus doing something new.

foo = “012345678”
foo[-4,-2] #-> “43”
foo[-3,-2] = “” #-> “0123678”
foo[-4,-3] = “9” #-> “93678”
foo[1,-2] = “123” #-> “3213678”
foo = “01295678”
foo[-4,-1] = “43” #-> “012345678”

In other words,

foo = “012345678”
foo[4,1] #-> 4
foo[4,-1] #-> 3

Is this making sense?

Charlie

2006/6/11, Charlie [email protected]:

Hi,

Just a logic question about substrings by position.

foo = “012345678”
foo[5,1] #-> “5”
foo[-4,2] #-> “56”
foo[-3,2] = “” #-> “0123458”

What version are you on?

foo = “012345678”
=> “012345678”
foo[-3,2]
=> “67”
RUBY_VERSION
=> “1.8.3”

Note also that you can use a range as well:

foo[-3…-1]
=> “678”
foo[-3…-1]
=> “67”

Shouldn’t a negative indexing work as well? (Below are just examples,
they currently don’t work) It will allow strings to be REVERSED easily,
and thus doing something new.

Negative indexes do work already. You probably mean negative lengths.

foo = “012345678”
foo[4,1] #-> 4
foo[4,-1] #-> 3

Is this making sense?

Dunno. I personally never missed this behavior. I’m not sure whether
there is much use for this and if it’s worth the effort. Note that the
implementation of String#[] becomes even more complex than it is right
now plus with the current design returned strings usually share the
buffer with the original. You cannot do that with implicit reversed
strings. I opt for a mild “no” but I’m not religious.

Kind regards

robert

On Jun 11, 2006, at 13:08, Charlie wrote:

Shouldn’t a negative indexing work as well? (Below are just
examples,
they currently don’t work) It will allow strings to be REVERSED
easily,
and thus doing something new.

Negative indexes do work already. You probably mean negative
lengths.

Yes, you’re right. It just mean lengths with direction.

I don’t think that having negative lengths reflect direction is at
all a natural decision; it’s describing the bounds of a substring,
not describing and implicit iteration through individual characters.
In other words, if negative lengths worked, I’d fully expect them to
work like this (y is negative): foo[x, y] == foo[(x+y), y.abs]

e.g.

foo = “0123456789”
foo[4,-1] # => 3
foo[3, 1] # => 3
foo[4,-2] # => 23
foo[2, 2] # => 23

Conflating string reversal into setting the bounds of the substring
is a mistake, I’d say, particularly since there’s already a perfectly
reasonable String#reverse.

matthew smillie.

Matthew S. wrote:

On Jun 11, 2006, at 13:08, Charlie wrote:

Shouldn’t a negative indexing work as well? (Below are just
examples,
they currently don’t work) It will allow strings to be REVERSED
easily,
and thus doing something new.

Negative indexes do work already. You probably mean negative
lengths.

Yes, you’re right. It just mean lengths with direction.

I don’t think that having negative lengths reflect direction is at
all a natural decision; it’s describing the bounds of a substring,
not describing and implicit iteration through individual characters.
In other words, if negative lengths worked, I’d fully expect them to
work like this (y is negative): foo[x, y] == foo[(x+y), y.abs]

e.g.

foo = “0123456789”
foo[4,-1] # => 3
foo[3, 1] # => 3
foo[4,-2] # => 23
foo[2, 2] # => 23

Conflating string reversal into setting the bounds of the substring
is a mistake, I’d say, particularly since there’s already a perfectly
reasonable String#reverse.

matthew smillie.

  I gather your opinion. And I'm sure that you know, there's always 

more than one way of achieving the same result. But having said that, it
would be better if the program can reflect, and at the sametime when
writing it, record accurately how a process actually works, assuming
that you’re modelling a real life process. In other words, you can
actually guess what’s acutally going on by just reading your codes.
This is esp true and important in modelling business. Again, just for
discussion purposes.

Charlie

Robert K. wrote:

2006/6/11, Charlie [email protected]:

Hi,

Just a logic question about substrings by position.

foo = “012345678”
foo[5,1] #-> “5”
foo[-4,2] #-> “56”
foo[-3,2] = “” #-> “0123458”

What version are you on?

1.8.4

You have probably missed out something, its foo[-3,2] = “” not
foo[-3,2]

foo = “012345678”
=> “012345678”
foo[-3,2]
=> “67”
RUBY_VERSION
=> “1.8.3”

Note also that you can use a range as well:

foo[-3…-1]
=> “678”
foo[-3…-1]
=> “67”

Shouldn’t a negative indexing work as well? (Below are just examples,
they currently don’t work) It will allow strings to be REVERSED easily,
and thus doing something new.

Negative indexes do work already. You probably mean negative lengths.

Yes, you’re right. It just mean lengths with direction.

foo = “012345678”
foo[4,1] #-> 4
foo[4,-1] #-> 3

Is this making sense?

Dunno. I personally never missed this behavior. I’m not sure whether
there is much use for this and if it’s worth the effort. Note that the
implementation of String#[] becomes even more complex than it is right
now plus with the current design returned strings usually share the
buffer with the original. You cannot do that with implicit reversed
strings. I opt for a mild “no” but I’m not religious.

I just thought that if the current behaviour is so useful, then it’ll
be equally useful to have an equal and opposite behaviour. It’s just
unnatural that things can only go one direction. Just a thought.

Kind regards

robert

Robert K. wrote:

2006/6/11, Charlie [email protected]:

What version are you on?

1.8.4

You have probably missed out something, its foo[-3,2] = “” not
foo[-3,2]

Right. But with assignment I would find it even more irritating to
have reversal - because the only thing that would make sense is to
apply the reversal to the assigned string - and this is extremely
unobvious.

I just thought that if the current behaviour is so useful, then it’ll
be equally useful to have an equal and opposite behaviour. It’s just
unnatural that things can only go one direction. Just a thought.

I don’t subscribe to this general statement. Program code also flows
only one direction and it would be more than unnatural to have it both
ways.

Kind regards

robert

Thanks for your comments. I think you all got the gist of the idea, so I
needn’t argue more. Personally, I think it has great potential for
genome sequencing and genetic algorithm to name a few.

2006/6/11, Charlie [email protected]:

What version are you on?

1.8.4

You have probably missed out something, its foo[-3,2] = “” not
foo[-3,2]

Right. But with assignment I would find it even more irritating to
have reversal - because the only thing that would make sense is to
apply the reversal to the assigned string - and this is extremely
unobvious.

I just thought that if the current behaviour is so useful, then it’ll
be equally useful to have an equal and opposite behaviour. It’s just
unnatural that things can only go one direction. Just a thought.

I don’t subscribe to this general statement. Program code also flows
only one direction and it would be more than unnatural to have it both
ways.

Kind regards

robert