Solved

return first character

Posted on 2000-03-11
13
201 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

770 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