Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

last 3 chars

Posted on 2000-02-17
3
171 Views
Last Modified: 2010-03-05
how do i assign a new variable to be the last three characters of a variable

ie if $old is "hello.txt"
then $new becomes "txt"

thanks
0
Comment
Question by:boofulls
3 Comments
 

Accepted Solution

by:
spreq earned 50 total points
ID: 2530734
1. get total character
2. simple mathematic to get location for the first of last three character
3. use substring

eg
$old = "hello.txt";
$long=length($old);   #total char
$FirstOfThree=$long-3;  #simple math
$new=substr($FirstOfThree,0,3); #get the last char



0
 

Author Comment

by:boofulls
ID: 2530779
even though there was an error in the last line....;)
0
 
LVL 84

Expert Comment

by:ozo
ID: 2533152
$new=substr($old,-3);  #unless what you really want are all characters after the last '.'
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question