CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Extracting a character from a variable

 



RayStreet
Deleted

Jan 24, 2000, 4:25 PM

Post #1 of 3 (610 views)
Extracting a character from a variable Can't Post

Now here's a simple problem.

How do I extract the nth character in a scalar variable and similarly, a group of characters (for instance, characters 4 to 6).

Ray


Jasmine
Administrator / Moderator

Jan 24, 2000, 6:30 PM

Post #2 of 3 (610 views)
Re: Extracting a character from a variable [In reply to] Can't Post

Just use the substr (substring) function, which uses the following format:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$extracted = substr(STRING,POSITION,LENGTH);
</pre><HR></BLOCKQUOTE>

The STRING is pretty self explanatory -- that's the string that you want to extract characters from.

POSITION is the starting point of extraction from the string. The count starts at 0, not 1.

LENGTH is the number of characters to extract.

Example:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$string = "Perl is cool.";

$extracted = substr($string,5,2);
# $extracted contains "is"
</pre><HR></BLOCKQUOTE>

Good luck!


RayStreet
Deleted

Jan 24, 2000, 7:18 PM

Post #3 of 3 (610 views)
Re: Extracting a character from a variable [In reply to] Can't Post

Thanks Jasmine

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives