LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   "perl help" (https://www.linuxquestions.org/questions/programming-9/perl-help-618561/)

sharath.bv 02-04-2008 07:12 AM

"perl help"
 
Hi all,
How to select the first word from a scalar variable.

ex: $a = "Hi all" :study:

Su-Shee 02-04-2008 07:59 AM

There's a million solutions to this problem, depending on what you're going to do afterwards - you may use split for example and put all words into an array or use a reg ex and just grab the first word ..

@allwords = split(' ', $yourscalar); <-- split at spaces
print $allwords[0]; <-- just the first word

($firstword) = ($yourscalar =~ m/(\w+)/); <-- match first word, capture it and put it into $firstword


Something like this...

sharath.bv 02-05-2008 03:54 AM

Thanks all i got the answer.....

sharath.bv 02-05-2008 03:55 AM

:)Thanks all i got the answer.....

sundialsvcs 02-05-2008 06:05 PM

The mantra in Perl-land is "TMTOWTDI" = There's More Than One Way To Do It.

:rolleyes:


All times are GMT -5. The time now is 05:35 PM.