Solved

return first character

Posted on 2000-03-11
13
181 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:mmcw
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 16

Expert Comment

by:maneshr
Comment Utility
ok then change it this way

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

Author Comment

by:mmcw
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
did the last solution help??

if not let me know. :)
0
 

Author Comment

by:mmcw
Comment Utility
It did!!!

Thank you again!!
0
 
LVL 16

Expert Comment

by:maneshr
Comment Utility
glad to know that :-)


Regards
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now