how to get the first word of a string 
Author Message
 how to get the first word of a string

   Given a string
       $string="this is a string";
I use the following
       $first_word=split(/s+/, $string);
to get the first word. However, I couldn't get it. Can anyone tell me
what's wrong?


Tue, 24 Aug 2004 00:30:05 GMT  
 how to get the first word of a string

Quote:
>    Given a string
>        $string="this is a string";
> I use the following
>        $first_word=split(/s+/, $string);
> to get the first word. However, I couldn't get it. Can anyone tell me
> what's wrong?

Two things.

1) Your pattern.  /s+/ splits on one or more literal s  I think you mean
/\s+/ to split on one or more white space.

2) Context. By assigning to a single scalar '$first_word' you are calling
split in scalar context, in which case it returns the number of elements in
the list.  By calling in list (or array) context you return the list and
assign those elements to the list on the left.  The brackets force list
context.

so

($first_word)=split(/\s+/, $string);

returns a list and puts the first item into $first_word, and discards the
rest.

Wyzelli
--
$j='on the job';$h='hacker';$p='Perl';for(reverse(1..100)){
$s=($_!=1)?'s':'';$t=($_!=1)?'one of those':'that';
print"$_ $h$s of $p $j,\n$_ $h$s of $p.\nIf $t $h$s should code a bug,\n";
$_--;$s=($_==1)?'':'s';print"There'd be $_ $h$s of $p $j!\n\n";}print"*use
strict*";



Tue, 24 Aug 2004 00:52:13 GMT  
 how to get the first word of a string
Quote:

>   Given a string
>       $string="this is a string";
>I use the following
>       $first_word=split(/s+/, $string);

                           ^^
                           ^^

You want to match one or more "s" characters?

Please to not attempt to retype code. Use copy/paste, else you'll
just get wise ass comments about your typos instead of about
your real question.

Quote:
>to get the first word. However, I couldn't get it.

Can you identify what it is that you _did_ get back from split()?

Quote:
>Can anyone tell me
>what's wrong?

Sure. Check the Posting Guidelines[1]. Enable warnings (it would
have said something about your use of split). And understand
"context".

See the "Context" section in:

   perldoc perldata

You are using split() in a "scalar context",

   perldoc -f split

to see what it will return in that case. That isn't what you want.

You want "list context" behavior, so arrange to call split in
list context:

   ($first_word) = split(/\s+/, $string);

or

  $first_word = (split(/\s+/, $string))[0];  # list slice

[1] http://mail.augustmail.com/~tadmc/clpmisc.shtml

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Tue, 24 Aug 2004 00:55:47 GMT  
 how to get the first word of a string
   Thank you so much for the help. By the way, my string has actually
some blank in the beginning, that is
    $string="  This is my string";
Then
    ($first_word)=split(/\s+/, $string);
gives me blank instead of the fisrt word. Any hint? Thanks again.
Quote:



> >    Given a string
> >        $string="this is a string";
> > I use the following
> >        $first_word=split(/s+/, $string);
> > to get the first word. However, I couldn't get it. Can anyone tell me
> > what's wrong?

> Two things.

> 1) Your pattern.  /s+/ splits on one or more literal s  I think you mean
> /\s+/ to split on one or more white space.

> 2) Context. By assigning to a single scalar '$first_word' you are calling
> split in scalar context, in which case it returns the number of elements in
> the list.  By calling in list (or array) context you return the list and
> assign those elements to the list on the left.  The brackets force list
> context.

> so

> ($first_word)=split(/\s+/, $string);

> returns a list and puts the first item into $first_word, and discards the
> rest.

> Wyzelli
> --
> $j='on the job';$h='hacker';$p='Perl';for(reverse(1..100)){
> $s=($_!=1)?'s':'';$t=($_!=1)?'one of those':'that';
> print"$_ $h$s of $p $j,\n$_ $h$s of $p.\nIf $t $h$s should code a bug,\n";
> $_--;$s=($_==1)?'':'s';print"There'd be $_ $h$s of $p $j!\n\n";}print"*use
> strict*";



Tue, 24 Aug 2004 01:24:00 GMT  
 how to get the first word of a string
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Quote:
>    Thank you so much for the help. By the way, my string has actually
> some blank in the beginning, that is
>     $string="  This is my string";
> Then
>     ($first_word)=split(/\s+/, $string);
> gives me blank instead of the fisrt word. Any hint? Thanks again.

Maybe it's time you consider a regular expression

($first_word) = $string =~ /(\w+)/;

for more info see "perldoc perlre" or
http://www.perldoc.com/perl5.6.1/pod/perlre.html

Quote:




> > >    Given a string
> > >        $string="this is a string";
> > > I use the following
> > >        $first_word=split(/s+/, $string);
> > > to get the first word. However, I couldn't get it. Can anyone tell me
> > > what's wrong?

> > Two things.

> > 1) Your pattern.  /s+/ splits on one or more literal s  I think you
> > mean /\s+/ to split on one or more white space.

> > 2) Context. By assigning to a single scalar '$first_word' you are
> > calling split in scalar context, in which case it returns the number of
> > elements in the list.  By calling in list (or array) context you return
> > the list and assign those elements to the list on the left.  The
> > brackets force list context.

> > so

> > ($first_word)=split(/\s+/, $string);

> > returns a list and puts the first item into $first_word, and discards
> > the rest.

> > Wyzelli
> > --
> > $j='on the job';$h='hacker';$p='Perl';for(reverse(1..100)){
> > $s=($_!=1)?'s':'';$t=($_!=1)?'one of those':'that';
> > print"$_ $h$s of $p $j,\n$_ $h$s of $p.\nIf $t $h$s should code a
> > bug,\n"; $_--;$s=($_==1)?'':'s';print"There'd be $_ $h$s of $p
> > $j!\n\n";}print"*use strict*";

-----BEGIN PGP SIGNATURE-----
Version: 6.5.8ckt
Comment: MY PUB KEY: http://thecouch.homeip.net:81/MinaNaguib.txt

iQA/AwUBPIgOU3kvfaRjIn+sEQIA3wCeLP+XsO7o6bOPh5ewhxkjnXZg9p0AoM6f
Ly7u6tmtYNfcXYKKjymYOCj8
=G0XY
-----END PGP SIGNATURE-----



Tue, 24 Aug 2004 02:05:24 GMT  
 how to get the first word of a string
On Thu, 07 Mar 2002 19:24:00 -0500, Zimmen Gnauh

[post reordered]

Quote:



>> >    Given a string
>> >        $string="this is a string";
>> > I use the following
>> >        $first_word=split(/s+/, $string);
>> > to get the first word. However, I couldn't get it. Can anyone tell me
>> > what's wrong?

>> Two things.

>> 1) Your pattern.  /s+/ splits on one or more literal s  I think you mean
>> /\s+/ to split on one or more white space.

>> 2) Context. By assigning to a single scalar '$first_word' you are calling
>> split in scalar context, in which case it returns the number of elements in
>> the list.  By calling in list (or array) context you return the list and
>> assign those elements to the list on the left.  The brackets force list
>> context.

>> so

>> ($first_word)=split(/\s+/, $string);

>> returns a list and puts the first item into $first_word, and discards the
>> rest.

>    Thank you so much for the help. By the way, my string has actually
> some blank in the beginning, that is
>     $string="  This is my string";
> Then
>     ($first_word)=split(/\s+/, $string);
> gives me blank instead of the fisrt word. Any hint? Thanks again.

So now it's time to read the documentation for the function you're
using.  It discusses this behavior and how the *default* behavior of
split() without a pattern *skips* leading spaces.  

Pay particular attention to the mention of the special case for
PATTERN.  

  perldoc -f split

--
Garry Williams



Tue, 24 Aug 2004 02:05:55 GMT  
 how to get the first word of a string


Quote:



> > >    Given a string
> > >        $string="this is a string";
> > > I use the following
> > >        $first_word=split(/s+/, $string);
> > > to get the first word. However, I couldn't get it. Can anyone tell me
> > > what's wrong?

> > Two things.

> > 1) Your pattern.  /s+/ splits on one or more literal s  I think you mean
> > /\s+/ to split on one or more white space.

> > 2) Context. By assigning to a single scalar '$first_word' you are
calling
> > split in scalar context, in which case it returns the number of elements
in
> > the list.  By calling in list (or array) context you return the list and
> > assign those elements to the list on the left.  The brackets force list
> > context.

> > so

> > ($first_word)=split(/\s+/, $string);

> > returns a list and puts the first item into $first_word, and discards
the
> > rest.

>    Thank you so much for the help. By the way, my string has actually
> some blank in the beginning, that is
>     $string="  This is my string";
> Then
>     ($first_word)=split(/\s+/, $string);
> gives me blank instead of the fisrt word. Any hint? Thanks again.

Post re-ordered:

Several Ways:



Tue, 24 Aug 2004 03:59:00 GMT  
 how to get the first word of a string


Quote:



> > >    Given a string
> > >        $string="this is a string";
> > > I use the following
> > >        $first_word=split(/s+/, $string);
> > > to get the first word. However, I couldn't get it. Can anyone tell me
> > > what's wrong?

> > Two things.

> > 1) Your pattern.  /s+/ splits on one or more literal s  I think you mean
> > /\s+/ to split on one or more white space.

> > 2) Context. By assigning to a single scalar '$first_word' you are
calling
> > split in scalar context, in which case it returns the number of elements
in
> > the list.  By calling in list (or array) context you return the list and
> > assign those elements to the list on the left.  The brackets force list
> > context.

> > so

> > ($first_word)=split(/\s+/, $string);

> > returns a list and puts the first item into $first_word, and discards
the
> > rest.

>    Thank you so much for the help. By the way, my string has actually
> some blank in the beginning, that is
>     $string="  This is my string";
> Then
>     ($first_word)=split(/\s+/, $string);
> gives me blank instead of the fisrt word. Any hint? Thanks again.

Post re-ordered.

Several ways.

skip the first element:

(undef, $first_word)=split(/\s+/, $string);

take a list slice for the particular value you require:

$first_word=(split(/\s+/, $string))[1];

strip the leading whitespace first (a FAQ):

$string =~ s/^\s*//;
($first_word)=split(/\s+/, $string);

use a regex:
$first_word = $1 if $string =~ m/(\w+)/;

And there would be heaps of others.  TMTOWTDI!

Wyzelli
--
$j='on the job';$h='hacker';$p='Perl';for(reverse(1..100)){
$s=($_!=1)?'s':'';$t=($_!=1)?'one of those':'that';
print"$_ $h$s of $p $j,\n$_ $h$s of $p.\nIf $t $h$s should code a bug,\n";
$_--;$s=($_==1)?'':'s';print"There'd be $_ $h$s of $p $j!\n\n";}print"*use
strict*";



Tue, 24 Aug 2004 04:04:35 GMT  
 how to get the first word of a string

Quote:



>>     ($first_word)=split(/\s+/, $string);

>Maybe it's time you consider a regular expression

>($first_word) = $string =~ /(\w+)/;

But that doesn't really do the same thing, for that you'd need:

   ($first_word) = $string =~ /(\S+)/;

[snip upside-down quoted text. Please don't do that]

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Tue, 24 Aug 2004 04:57:39 GMT  
 how to get the first word of a string
On Fri, 8 Mar 2002 12:34:35 +0930,

Quote:



>>    Thank you so much for the help. By the way, my string has actually
>> some blank in the beginning, that is
>>     $string="  This is my string";
>> Then
>>     ($first_word)=split(/\s+/, $string);
>> gives me blank instead of the fisrt word. Any hint? Thanks again.

> Post re-ordered.

> Several ways.

[snip of several suggestions]

Or you could split on a literal space:

($first_word) = split ' ', $string;

$ perldoc -f split
[snip]
               As a special case, specifying a PATTERN of space
               ("' '") will split on white space just as "split"
               with no arguments does.  Thus, "split(' ')" can be
               used to emulate awk's default behavior, whereas
               "split(/ /)" will give you as many null initial
               fields as there are leading spaces.  A "split" on
               "/\s+/" is like a "split(' ')" except that any
               leading whitespace produces a null first field.  A
               "split" with no arguments really does a "split('
               ', $_)" internally.
[snip]

Martien
--
                        |
Martien Verbruggen      | My friend has a baby. I'm writing down all
Trading Post Australia  | the noises the baby makes so later I can ask
                        | him what he meant - Steven Wright



Tue, 24 Aug 2004 05:08:32 GMT  
 how to get the first word of a string
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Quote:



> >>     ($first_word)=split(/\s+/, $string);

> >Maybe it's time you consider a regular expression

> >($first_word) = $string =~ /(\w+)/;

> But that doesn't really do the same thing, for that you'd need:

>    ($first_word) = $string =~ /(\S+)/;

This will get the first non-whitespace collection.  The OP is trying to get
the first word therefore \w is more appropriate.

Example:
$string = " 'Tom, how bizarre' said Margaret";

There's a good chance he does not want  the ' and the , touching "Tom".

. . . but then again, maybe he does.  The original request wasn't too
detailed except that it wanted the first word.

-----BEGIN PGP SIGNATURE-----
Version: 6.5.8ckt
Comment: MY PUB KEY: http://thecouch.homeip.net:81/MinaNaguib.txt

iQA/AwUBPIg63XkvfaRjIn+sEQKhygCgt7LQ2bYap2BlyR8aefDMloHcD0YAoNgc
crPBnrIKnXoZV8aDvm/Nggx3
=mYxk
-----END PGP SIGNATURE-----



Tue, 24 Aug 2004 05:15:26 GMT  
 how to get the first word of a string

[...]

Quote:
> ($first_word)=split(/\s+/, $string);

> returns a list and puts the first item into $first_word, and discards the
> rest.

To be pedantically correct, "discard" isn't quite right.  When assigning
split() to a list, Perl silently supplies a limit of one more than
there are list elements (unless an explicit limit is given, of course).

A limit of exactly the number of elements isn't good enough, because
then the final element could contain the rest of the string, even if
another split point follows.

Anno



Tue, 24 Aug 2004 13:37:40 GMT  
 how to get the first word of a string

Quote:

> > ($first_word)=split(/\s+/, $string);

or: $first_word = (split/\s+/,$string)[0];

that way you can easily retreive the second word aswell:

$second_word = (split/\s+/,$string)[1];



Fri, 27 Aug 2004 12:50:09 GMT  
 how to get the first word of a string

Quote:

>    Given a string
>        $string="this is a string";
> I use the following
>        $first_word=split(/s+/, $string);
> to get the first word. However, I couldn't get it. Can anyone tell me
> what's wrong?

split returns a list (array)
Also your regular expression is spluitting on ("s", "ss", "sss", ...)
try:


then the elements of word ($word[$i]) have the values:

$words[0] ----> "this"
$words[1] ----> "is"
$words[2] ----> "a"
$words[3] ----> "string"

Michael Witkowski



Sun, 29 Aug 2004 17:02:25 GMT  
 how to get the first word of a string

Quote:

> $string = "this is a string";

> then the elements of word ($word[$i]) have the values:

> $words[0] ----> "this"
> $words[1] ----> "is"
> $words[2] ----> "a"
> $words[3] ----> "string"

Also note that split has special case behaviour which you can use to carve
a line into chunks delimited by spaces:

  DB<1> $string = '  This is a string too'



   0  ''
   1  'This'
   2  'is'
   3  'a'
   4  'string'
   5  'too'
)


   0  'This'
   1  'is'
   2  'a'
   3  'string'
   4  'too'
)
  DB<6>

See the description of split in the perlfunc man page for more info.

Hope this helps,

Mike

--

http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA

http://www.starnix.com/            |                  75D2 9EC4 C1C0 0599 13DA



Sun, 29 Aug 2004 18:23:29 GMT  
 
 [ 15 post ] 

 Relevant Pages 

1. getting first ten words from array

2. extract the first word in a string

3. Getting the First Letter in string

4. getting first letter of a string

5. Getting words between two strings

6. Getting the first occurence of a string

7. How to Capitalize the first letter of a word an or whole word

8. Getting words between two other words within an array

9. Capitalising First Letters Of Words

10. How can I convert all but the first char in a word to lower case?======

11. How to I changs first letter of all words in caps and remaining in small

12. Stripping out all but the first word

 

 
Powered by phpBB® Forum Software