Deleting characters from the end of each line

A

amrussell

I am trying to figure out how I can go to the end of each line in a
report and delete an exact number of characters from right to left. I
have some characters at the end of each line in some reports I am
processing and I want to get rid of them. I know how to do a search
and replace, but the patterns are never the same. Can any one help?
 
P

Paul Lalli

amrussell said:
I am trying to figure out how I can go to the end of each line in a
report and delete an exact number of characters from right to left. I
have some characters at the end of each line in some reports I am
processing and I want to get rid of them. I know how to do a search
and replace, but the patterns are never the same. Can any one help?

s/// is useful to remove specific patterns.
substr() is useful to remove specific numbers of characters.

substr($s, -3) = q{};
for example, will remove the last three characters of $s.

perldoc -f substr
for more substr tricks.

Paul Lalli
 
T

Tad McClellan

amrussell said:
I am trying to figure out how I can go to the end of each line in a
report and delete an exact number of characters from right to left.


perldoc -f substr

Can any one help?

substr($str, -$exact_number_of_characters, length $str, '');
 
U

Uri Guttman

PL> s/// is useful to remove specific patterns.
PL> substr() is useful to remove specific numbers of characters.

PL> substr($s, -3) = q{};
PL> for example, will remove the last three characters of $s.

i like 4 arg substr and it is faster than lvalue substr:

substr( $s, -3, 3, '' ) ;

the biggest annoyance is there is no way to do a wildcard length to the
end of the string so you have to pass in a proper length value.

uri
 
P

Paul Lalli

Uri said:
PL> substr($s, -3) = q{};
PL> for example, will remove the last three characters of $s.

i like 4 arg substr and it is faster than lvalue substr:

substr( $s, -3, 3, '' ) ;

the biggest annoyance is there is no way to do a wildcard length to the
end of the string so you have to pass in a proper length value.

I knew *someone* would bring this up. :) While I know about the
4-arg substr, I've just never gotten the hang of reading it correctly,
and it always causes me to do a doubletake. Even though the concept of
a lvalue subroutine is weird, that version of it "reads" correctly to
me. ("Make this substring of the string into this").

TIMTOWTDI, and all...

Paul Lalli
 
J

John W. Krahn

Tad said:
perldoc -f substr



substr($str, -$exact_number_of_characters, length $str, '');

ITYM:

substr($str, -$exact_number_of_characters, $exact_number_of_characters, '');


John
 
U

Uri Guttman

PL> substr($s, -3) = q{};
PL> for example, will remove the last three characters of $s.
PL> I knew *someone* would bring this up. :) While I know about the
PL> 4-arg substr, I've just never gotten the hang of reading it correctly,
PL> and it always causes me to do a doubletake. Even though the concept of
PL> a lvalue subroutine is weird, that version of it "reads" correctly to
PL> me. ("Make this substring of the string into this").

i will give you a big help on grokking 4 arg substr. it has the SAME API
as splice but for strings. splice always has had the 4th arg (actually a
list) to replace the list you splice out. the only main diff is that
splice removes elements but substr only copies them. both can be used to
insert stuff (without deleting) in the middle of their targets as well

a very rare but interesting use of substr is that you can take a ref to
it and pass that around to stuff like s///. splice can't do that.

uri
 
B

bytebro

Uri said:
PL> I knew *someone* would bring this up. :) While I know about the
PL> 4-arg substr, I've just never gotten the hang of reading it correctly,
PL> and it always causes me to do a doubletake. Even though the concept of

i will give you a big help on grokking 4 arg substr. it has the SAME API

I thought this looked odd. I just checked in my copy of the Camel book
(2nd Ed), and AFAICT, there's no such beast as a 4 argument substr.
Shows how out of touch I am, I guess. Would I be correct in assuming
this is a relatively recent addition?
 
P

Paul Lalli

bytebro said:
I thought this looked odd. I just checked in my copy of the Camel book
(2nd Ed), and AFAICT, there's no such beast as a 4 argument substr.
Shows how out of touch I am, I guess. Would I be correct in assuming
this is a relatively recent addition?

Not really, no.

http://www.perl.com/doc/manual/html/pod/perldelta.html#4th_argument_to_substr

New as of Perl 5.005, which was released in 1998. I don't know what
version the 2nd edition of the Camel covers. You should really
consider upgrading to the third edition (which is still only 5.6, but
better than nothing...)

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

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top