the opposite of 'chop'

M

Mark Tarver

I understand 'chop' grabs the last token of a string. What grabs the
first?

Mark
 
P

Paul Lalli

Mark said:
I understand 'chop' grabs the last token of a string.

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.
What grabs the first?

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
 
H

Henry Law

Paul said:
my $first_char = substr($string,0,1, q{});

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

Mark Tarver

thanks - new question posted.

Mark

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
 
P

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?

Because '' looks far too much like " and that can be confusing to
people reading the code.

Paul Lalli
 
J

John Bokma

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{});

or reverse the string, chop it, and reverse it back :)
 
B

Big and Blue

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.

Why is there anything there at all?

my $first_char = substr($string, 0, 1);

does the job without any confusion.
 
B

Ben Morrow

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.

No, that's not the same as a 'reverse chop'. The version given deletes
the first char of the string, as well as returning it.

Ben
 
P

Paul Lalli

Big said:
Why is there anything there at all?

my $first_char = substr($string, 0, 1);

does the job without any confusion.

Only if you define "the job" to mean "half of what chop() does", which
was the exact mistake the OP made when I answered him too.

Paul Lalli
 

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,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top