question about substr

C

Chuck

I am a new perl learner and have a question about substr

Does the starting string position x in substr("string",x,y) need to be set
to zero to start reading at the 1st postion or set to one? I found some
examples that seem to show it both ways - am I assuming correctly that the
starting postion parameter is always one less than the position you want to
start at?
 
T

Tore Aursand

I am a new perl learner and have a question about substr
[...]

Does it really take longer to check the documentation than to post a
message here _and_ wait for an answer?

Please use the documentation as much as possible, and in this case:

perldoc -f substr
Does the starting string position x in substr("string",x,y) need to be
set to zero to start reading at the 1st postion or set to one?

"Everything" is zero-based in Perl;

my $orig = 'abcd';
my $ex_1 = substr( $orig, 0, 4 ); # 'abcd'
my $ex_2 = substr( $orig, 1, 4 ); # 'bcd'
I found some examples that seem to show it both ways - am I assuming
correctly that the starting postion parameter is always one less than
the position you want to start at?

Don't _assume_ anything when it's so easy to look it up in the Perl
documentation.

And your statement is wrong, as I read it: The index (position) of the
first character in a string is 0, not 1.
 
B

Brad Baxter

I am a new perl learner and have a question about substr

Does the starting string position x in substr("string",x,y) need to be set
to zero to start reading at the 1st postion or set to one? I found some
examples that seem to show it both ways - am I assuming correctly that the
starting postion parameter is always one less than the position you want to
start at?

What did you see when you tried it?
 
C

Chuck

I checked the docs and the book "perl in a nutshell" and found two different
examples with conflicting data - that is why I posted here - I am a new
"perl" programmer - not a new programmer and would not have asked for
information if I could have found it elsewhere. I have been programming for
over 30 years and have never encountered such a bunch of self-centered
arrogant pricks as you guys in this newsgroup. Don't assume that each new
poster here is a wet-behind-the-ears computer geek.

--

regards,

Chuck

Tore Aursand said:
I am a new perl learner and have a question about substr
[...]

Does it really take longer to check the documentation than to post a
message here _and_ wait for an answer?

Please use the documentation as much as possible, and in this case:

perldoc -f substr
Does the starting string position x in substr("string",x,y) need to be
set to zero to start reading at the 1st postion or set to one?

"Everything" is zero-based in Perl;

my $orig = 'abcd';
my $ex_1 = substr( $orig, 0, 4 ); # 'abcd'
my $ex_2 = substr( $orig, 1, 4 ); # 'bcd'
I found some examples that seem to show it both ways - am I assuming
correctly that the starting postion parameter is always one less than
the position you want to start at?

Don't _assume_ anything when it's so easy to look it up in the Perl
documentation.

And your statement is wrong, as I read it: The index (position) of the
first character in a string is 0, not 1.
 
B

Brian McCauley

Chuck said:
I am a new perl learner and have a question about substr

The first thing to try: perldoc -f substr
Does the starting string position x in substr("string",x,y) need to be set
to zero to start reading at the 1st postion or set to one?

You could answer this yourself by reading the aforementioned
documentation - at least as far as the second sentence.
I found some examples that seem to show it both ways

Studying examples of function use should always be used in addition to
not in place of reading the description of the function in the
reference manuals.
am I assuming correctly that the starting postion parameter is
always one less than the position you want to start at?

No, not always, you can change it but as the aforementioned
documentation says "don't do that".

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
R

Richard Morse

Chuck said:
I am a new perl learner and have a question about substr

Does the starting string position x in substr("string",x,y) need to be set
to zero to start reading at the 1st postion or set to one? I found some
examples that seem to show it both ways - am I assuming correctly that the
starting postion parameter is always one less than the position you want to
start at?

substr uses a zero-based string. So the starting index is 0 to start at
the beginning of the string. In the (limited) context of substrs using
only 0 + positive integers, you can think of the index as how many
characters into the string to start (ie, substr($var, 1) will get you
everything after first skipping one character).

Ricky
 
B

Brad Baxter

I checked the docs and the book "perl in a nutshell" and found two different
examples with conflicting data - that is why I posted here - I am a new
"perl" programmer - not a new programmer and would not have asked for
information if I could have found it elsewhere. I have been programming for
over 30 years and have never encountered such a bunch of self-centered
arrogant pricks as you guys in this newsgroup. Don't assume that each new
poster here is a wet-behind-the-ears computer geek.

I guess you could say it's a winnowing process. Otherwise a bazillion new
posters would ask questions here instead of looking it up for themselves.
They do anyway.

Actually, it seems you got several answers to your question, some of which
pointed out the answer is in the documentation. And you got mine which
asked simply, what did you see when you tried it. I mean, trying it out
is so trivial.

I think your reaction is overblown, and you paint with an awfully broad
brush. I have been programming for over 20 years and have never
encountered such a bunch of selflessly helpful, intelligent, and
challenging folks as the guys and gals in this newsgroup.

Of course, there are some pricks, too, with whom I may be included
depending on your point of view.

Regards,

Brad
 
C

ctcgag

Chuck said:
I checked the docs and the book "perl in a nutshell" and found two
different examples with conflicting data - that is why I posted here

You should run the example you found in "perl in a nutshell" and see if it
produces the results it says it does. And if you are going to post here
about the conflicting examples, you should include an excerpt of the
examples to show the conflict. Otherwise we have no way of knowing if
there actually is a conflict, or if you are just misinterpreting it.
- I
am a new "perl" programmer - not a new programmer and would not have
asked for information if I could have found it elsewhere. I have been
programming for over 30 years

Well cripes, in that case you should damn well know better than to ask
others to resolve putative conflicts without first demonstrating them.
and have never encountered such a bunch of
self-centered arrogant pricks as you guys in this newsgroup. Don't assume
that each new poster here is a wet-behind-the-ears computer geek.

Some people have 30 years of experience. Some people have one year
of experience repeated 30 times.

Xho
 
T

Tad McClellan

Chuck said:
and have never encountered such a bunch of self-centered
arrogant pricks as you guys in this newsgroup.


We assume then that you won't be coming back.

Clearly, our loss is great.

poster here is a wet-behind-the-ears computer geek.


You've lurked here for several days and have not seen a significant
portion of wet-behind-the-ears posts?

Must have been a statistical anomaly.
 
R

Robin

Chuck said:
I am a new perl learner and have a question about substr

Does the starting string position x in substr("string",x,y) need to be set
to zero to start reading at the 1st postion or set to one? I found some
examples that seem to show it both ways - am I assuming correctly that the
starting postion parameter is always one less than the position you want to
start at?

RTFM.
and it's 0
-Robin
 
T

Tore Aursand

I checked the docs and the book "perl in a nutshell" and found two
different examples with conflicting data
[...]

Really? I don't have that book you refer to at hand, but it would be
interesting to see those examples.

Anyway. You _couldn't_ have looked it up in the Perl documentation.
What's unclear with the documentation for the 'substr' function? It says
very clear that strings are zero-based;

substr EXPR,OFFSET,LENGTH
substr EXPR,OFFSET
Extracts a substring out of EXPR and returns it. First charac-
ter is at offset 0, or whatever you've set $[ to (but don't do
that).

What is it that you don't understand with this? And - if you're still in
doubt - why don't try it yourself? It would certainly have given you the
answer much faster.

And: Please don't top-post. It's a bad thing.
 
B

Bart Lateur

Chuck said:
Does the starting string position x in substr("string",x,y) need to be set
to zero to start reading at the 1st postion or set to one? I found some
examples that seem to show it both ways - am I assuming correctly that the
starting postion parameter is always one less than the position you want to
start at?

I find it easier to think of it this way:

the offset x is the number of characters you want to skip.

So yes, to start at the very first position, this offset is 0.
 
A

Anno Siegel

Chuck said:
arrogant pricks as you guys in this newsgroup. Don't assume that each new
poster here is a wet-behind-the-ears computer geek.

We only assume what we can deduce from what you post.

Experienced programmers know how to look things up in the documentation.
Asking a question (except "where is the documentation") before doing that
marks you as a wet-behind-the-ears computer geek. That you have managed
to stay one through years of programming experience is remarkable, but
in no way unique. We have a few regular visitors of that type.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top