Computing.Net > Forums > Programming > Perl: Stripping last 4 characters from string

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Perl: Stripping last 4 characters from string

Reply to Message Icon

Name: jb60606
Date: July 1, 2009 at 12:27:06 Pacific
OS: Mac OS X 10.5.6
CPU/Ram: Octo 2.8Ghz Xeon
Product: Apple / MAC PRO
Subcategory: General
Comment:

How do I strip the last 4 characters of a string? I've been trying to use
substr, though since the length of what I want returned is variable, I
don't get desired results.

Let's say I have the following records:

AOX.B
LVZMZ.B
MQZ.Q
NQYUZ.B
PCZUX.B
QFZUZ.B
RTW.N
SKXVZ.B
SYZXV.W
UOZUX.N
BMZVZ.N
VRZ.Q
WPZUX.B
XOXRP.X
YMZMZ.N
ZZWMR.N
CLXVX.X
DHTVZ.X
EOYOZ.N
FZUVV.N
HFNPZ.X
IQZXQ.Q

I want to strip off the last four characters, and print what's left. It
should return the following:

A
LVZ
M
NQ
PC
QF
R
SKX
SYZ
UOZ
BMZ
V
WPZ
XOX
YMZ
ZZW
CLX
DHT
EOY
FZU
HFN
IQZ

Any hints?



Sponsored Link
Ads by Google

Response Number 1
Name: FishMonger
Date: July 1, 2009 at 17:32:26 Pacific
Reply:

Your answer is in the second sentence of the perldoc for the substr function.

perldoc -f substr


0

Response Number 2
Name: jb60606
Date: July 2, 2009 at 03:37:33 Pacific
Reply:

Thanks.

The info page for substr, was just confusing to me.

I got desired results with the following:

my $symbol = substr $line[0], 0, -4;


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Perl: Stripping last 4 characters from string

Get last 4 character from a file www.computing.net/answers/programming/get-last-4-character-from-a-file/19808.html

Remove last 2 characters from each line.... www.computing.net/answers/programming/remove-last-2-characters-from-each-line/20257.html

cmd.exe alternative from JPSoft? www.computing.net/answers/programming/cmdexe-alternative-from-jpsoft/17245.html