question about substr

Discussion in 'Perl Misc' started by Chuck, Apr 15, 2004.

  1. Chuck

    Chuck Guest

    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?

    --

    regards,

    Chuck
    Chuck, Apr 15, 2004
    #1
    1. Advertising

  2. Chuck

    Tore Aursand Guest

    On Thu, 15 Apr 2004 10:04:37 -0500, Chuck wrote:
    > 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.


    --
    Tore Aursand <>
    "To cease smoking is the easiset thing I ever did. I ought to know,
    I've done it a thousand times." (Mark Twain)
    Tore Aursand, Apr 15, 2004
    #2
    1. Advertising

  3. Chuck

    Brad Baxter Guest

    On Thu, 15 Apr 2004, Chuck wrote:

    > 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?
    Brad Baxter, Apr 15, 2004
    #3
  4. Chuck

    Chuck Guest

    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" <> wrote in message
    news:p...
    > On Thu, 15 Apr 2004 10:04:37 -0500, Chuck wrote:
    > > 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.
    >
    >
    > --
    > Tore Aursand <>
    > "To cease smoking is the easiset thing I ever did. I ought to know,
    > I've done it a thousand times." (Mark Twain)
    Chuck, Apr 15, 2004
    #4
  5. "Chuck" <> writes:

    > 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\\
    Brian McCauley, Apr 15, 2004
    #5
  6. In article <0wxfc.985$>,
    "Chuck" <> wrote:

    > 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

    --
    Pukku
    Richard Morse, Apr 15, 2004
    #6
  7. Chuck

    Brad Baxter Guest

    On Thu, 15 Apr 2004, Chuck wrote:

    > 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
    Brad Baxter, Apr 15, 2004
    #7
  8. Chuck

    Guest

    "Chuck" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Apr 15, 2004
    #8
  9. Chuck <> wrote:

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Apr 15, 2004
    #9
  10. Chuck

    Robin Guest

    "Chuck" <> wrote in message
    news:0wxfc.985$...
    > 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
    Robin, Apr 15, 2004
    #10
  11. Chuck

    Tore Aursand Guest

    On Thu, 15 Apr 2004 11:48:06 -0500, Chuck wrote:
    > 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.


    --
    Tore Aursand <>
    "There are three kinds of lies: lies, damn lies, and statistics."
    (Benjamin Disraeli)
    Tore Aursand, Apr 16, 2004
    #11
  12. Chuck

    Bart Lateur Guest

    Chuck wrote:

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

    --
    Bart.
    Bart Lateur, Apr 16, 2004
    #12
  13. Chuck

    Matt Garrish Guest

    "Robin" <robin @ infusedlight.net> wrote in message
    news:c5n4ap$ut8$...
    >
    > "Chuck" <> wrote in message
    > news:0wxfc.985$...
    > > 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
    >
    >


    For someone who's never bothered to read that same manual, your posturing is
    quite amusing.

    Matt
    Matt Garrish, Apr 16, 2004
    #13
  14. Chuck

    Anno Siegel Guest

    Chuck <> wrote in comp.lang.perl.misc:

    > 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
    Anno Siegel, Apr 16, 2004
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kees Hoogendijk

    getline and substr

    Kees Hoogendijk, Dec 20, 2003, in forum: C++
    Replies:
    4
    Views:
    551
    Kees Hoogendijk
    Dec 21, 2003
  2. entropy123

    [C++] Odd Problem with "substr"

    entropy123, Jun 1, 2005, in forum: C++
    Replies:
    7
    Views:
    543
    Stephen Howe
    Jun 2, 2005
  3. David Resnick
    Replies:
    1
    Views:
    307
    Victor Bazarov
    Apr 10, 2006
  4. Boltar
    Replies:
    2
    Views:
    475
    Boltar
    Apr 16, 2008
  5. Sean Nakasone
    Replies:
    10
    Views:
    171
    Dr.Ruud
    Oct 6, 2007
Loading...

Share This Page