Gossamer Forum
Home : General : Perl Programming :

Couple of Questions

Quote Reply
Couple of Questions
Hello,

I was wondering how to get the first character of a varible. For expamle if the varible $var contained the value test. How would you strip that varible down t.

How do I check to make sure that the first character in a varible only a letter or a number?

Thanks for your help,
Matt
Quote Reply
Re: Couple of Questions In reply to
To pick up just the first letter, use

substr($var,0,1)

The first number is the position in the string, starting with position 0 and the second number is the length of the string.

To test whether that character is a letter or number, use

substr($var,0,1) =~ /[0-9a-zA-Z]/

If you were willing to allow underscores, too, you could shorten it to

substr($var,0,1) =~ /\w/


------------------
JPD





Quote Reply
Re: Couple of Questions In reply to
I dont not want the first character to be anything but a number of a letter. I want it to remove and non letters or numbers from the first character. I used the code:
Code:
$var = $in{'test'} ;
substr($var,0,1) =~ /[0-9a-zA-Z]/ ;
$new_var = substr($var,0,1);
print "The new var is <b>$new_var</b>\n" ;
What do I have to modifty to do this?

-Matt
Quote Reply
Re: Couple of Questions In reply to
Code:
$var = $in{'test'};
unless (substr($var,0,1) =~ /[0-9a-zA-Z]/) {
substr($var,0,1) ="";
}
print "The variable is $var.";


------------------
JPD