M
Mark Tarver
I understand 'chop' grabs the last token of a string. What grabs the
first?
Mark
first?
Mark
Mark said:I understand 'chop' grabs the last token of a string.
What grabs the first?
Paul said:my $first_char = substr($string,0,1, q{});
Paul said:You understand incorrectly. chop() does not "grab" the last character
in a string. It removes the last character from the string, and then
returns whatever that character was. That is, it directly modifies the
string you pass to it.
There is no direct functional equivalent for the beginning of the
string. If you'd like, you can use substr:
my $first_char = substr($string,0,1, q{});
Paul Lalli
Henry said:I thought this was a typo until I looked up perlop; I see that q{} is
synonymous with ''. Is there a particular benefit from using q{}
rather than '' here?
Paul Lalli said:You understand incorrectly. chop() does not "grab" the last character
in a string. It removes the last character from the string, and then
returns whatever that character was. That is, it directly modifies the
string you pass to it.
There is no direct functional equivalent for the beginning of the
string. If you'd like, you can use substr:
my $first_char = substr($string,0,1, q{});
Henry said:I thought this was a typo until I looked up perlop; I see that q{} is
synonymous with ''. Is there a particular benefit from using q{}
rather than '' here? I understand that it's very convenient when
single-quoting a string that contains a single quote.
Quoth Big and Blue said:Why is there anything there at all?
my $first_char = substr($string, 0, 1);
does the job without any confusion.
Big said:Why is there anything there at all?
my $first_char = substr($string, 0, 1);
does the job without any confusion.
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.