Perl: Stripping last 4 characters from string

Apple / MAC PRO
July 1, 2009 at 12:27:06
Specs: Mac OS X 10.5.6, Octo 2.8Ghz Xeon
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?


See More: Perl: Stripping last 4 characters from string

Report •


#1
July 1, 2009 at 17:32:26
Your answer is in the second sentence of the perldoc for the substr function.

perldoc -f substr


Report •

#2
July 2, 2009 at 03:37:33
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;


Report •

Related Solutions


Ask Question