Solved

return first character

Posted on 2000-03-11
13
210 Views
Last Modified: 2010-03-05
Hello,

I have a lot of words like:

Login
Start
Enter

I want to use the ACCESSKEY command.
Therefor I need the first caracter of the words.

How to teturn the first caracter of for example:
$test = "Login";

greetings Michel
0
Comment
Question by:mmcw
  • 7
  • 6
13 Comments
 
LVL 16

Accepted Solution

by:
maneshr earned 50 total points
ID: 2608028
try this.

in addition to what you want i am also removing any leading spaces.


==========================================
#!/usr/local/bin/perl

$test="Login";

## Remove any leading space from the variable
$test=~ s/^\s+//g;

$first_char=substr($test,0,1); ## Get the first character!!


print $first_char;
0
 

Author Comment

by:mmcw
ID: 2608134
I have only a little question about the above:

I want the words to begin with:

<u>L</u>ogin

I do need the character between the <u> and the </u>

Can you help me again????
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2608163
sure!!


here goes..

change....

$first_char=substr($test,0,1); ## Get the first character!!

to .....

 $test=~ s/(^.)/<u>$1<\/u>/; ## Get the first char & put some tag before & after it

OR

$test="<u>".substr($test,0,1)."</u>".substr($test,1,length($test)); ## Get the first character & put some tag before & after it
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 

Author Comment

by:mmcw
ID: 2608201
Sorry,

My english was very bat!!!

What I want is this:

The words are:

<u>L</u>ogin
S<u>a</u><rt

What I now want is not the first character but the character between the tags <u> and </u>

The result of the first word:
<u>L</u>ogin has to be L

The result of the second word:
<u>L</u>ogin has to be a

Can you help me??
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2608250
no problem!! here it is.....


$test="<u>L</u>ogin";

## Remove any leading space from the variable
$test=~ s/^\s+//g;

## Remove the <u> & </u> tags
$test=~ s/<u>//;
$test=~ s/<\/u>//;

$first_char=substr($test,0,1); ## Get the first character!!

print $first_char;
0
 

Author Comment

by:mmcw
ID: 2608944
But what about St<u>a</u>rt
The result of the second word:
St<u>a</u>rt has to be a`???

How to make the script so that it will not search for the first character but for the character between the <u> and </u>
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2609014
ok then change it this way

## Remove the <u> & </u> tags
                   $test=~ s/<u>//g;
                   $test=~ s/<\/u>//g;
0
 

Author Comment

by:mmcw
ID: 2609904
Sorry, What do you mean.
How has the total script part to be??
When I change the part you say the result will be "S"

The result has to be "a" (the third character)

0
 
LVL 16

Expert Comment

by:maneshr
ID: 2609951
first i think you have be more clear with your question.

when you started off you mentioned about the first character, then you added an HTML tag, then you changed the position of that HTML tag.
now you dont just want to remove the tag, but also want to extract the character between the tag.

Anyway, here is what the following script will do.

it will look for the <u> </u> tag anwhere in a word. if the tag is found it will extract and show  you whatever is between that tag.



#!/usr/local/bin/perl

$test="Lo<u>g</u>in";

##Some more combinations that you can test with
##$test="Lo<u>g</U>in";
##$test="Logi<U>n</U>";

## Remove any leading space from the variable
$test=~ s/^\s+//g;

## Remove any trailing spaces from the variable
$test=~ s/\s+$//g;

## Search for the <u></u> or <U></U> tag & get anything between it.
$test=~ /<u>(.*)<\/u>/i;
$first_char=$1;

print $first_char;
0
 

Author Comment

by:mmcw
ID: 2611172
Sorry for being not clear.
The problem was that the first question did not solve my problem. Thats why I had to ask more!!
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2612117
did the last solution help??

if not let me know. :)
0
 

Author Comment

by:mmcw
ID: 2613023
It did!!!

Thank you again!!
0
 
LVL 16

Expert Comment

by:maneshr
ID: 2613123
glad to know that :-)


Regards
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

831 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