• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

return first character

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
mmcw
Asked:
mmcw
  • 7
  • 6
1 Solution
 
maneshrCommented:
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
 
mmcwAuthor Commented:
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
 
maneshrCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mmcwAuthor Commented:
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
 
maneshrCommented:
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
 
mmcwAuthor Commented:
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
 
maneshrCommented:
ok then change it this way

## Remove the <u> & </u> tags
                   $test=~ s/<u>//g;
                   $test=~ s/<\/u>//g;
0
 
mmcwAuthor Commented:
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
 
maneshrCommented:
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
 
mmcwAuthor Commented:
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
 
maneshrCommented:
did the last solution help??

if not let me know. :)
0
 
mmcwAuthor Commented:
It did!!!

Thank you again!!
0
 
maneshrCommented:
glad to know that :-)


Regards
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now