Re: Transforming a space-separated string to an array for words.

2005-01-13 Thread Bryan R Harris

> Robin <[EMAIL PROTECTED]> suggested:
>> This will split up the string based on the pattern (in this
>> case, a single space. You may want to change that to gain
>> robustness, e.g. /[ \t]+/ will split on any number of spaces and tabs)
> 
> I suggest /\s+/ instead. This splits on any whitespace,
> and it'll also remove trailing whitespace like those
> pesky trailing \n:


And further, use split(' ',$numbers), which is a special case that splits on
all whitespace, ignoring initial whitespace.  Just a bare "split" does that
by default.

Try this:

perl -e '$_ = "1 2 3 4 5  "; print "method 1:  ", join(",",split /\s+/),
"\n", "method 2: ", join(",",split), "\n";'

- B


RE: Transforming a space-separated string to an array for words.

2005-01-13 Thread Thomas Bätzler
Robin <[EMAIL PROTECTED]> suggested:
> This will split up the string based on the pattern (in this 
> case, a single space. You may want to change that to gain 
> robustness, e.g. /[ \t]+/ will split on any number of spaces and tabs)

I suggest /\s+/ instead. This splits on any whitespace,
and it'll also remove trailing whitespace like those
pesky trailing \n:


#!/usr/bin/perl -w

use strict;

my $string = "foo baz bar\n";

foreach my $word (split/[ \t]+/, $string) {
  print "'$word'\n";
}

foreach my $word ( split/\s+/, $string ) {
  print "'$word'\n";
}


HTH,
Thomas

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Transforming a space-separated string to an array for words.

2005-01-12 Thread Robin
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

On Thu, 13 Jan 2005 20:02, Harold Castro wrote:
> my $numbers = ;
> my @array = $numbers;
Amend to:
my @array = split(/ /,$numbers);

This will split up the string based on the pattern (in this case, a single 
space. You may want to change that to gain robustness, e.g. /[ \t]+/ will 
split on any number of spaces and tabs)

- -- 
Robin <[EMAIL PROTECTED]> JabberID: <[EMAIL PROTECTED]>

Hostes alienigeni me abduxerunt. Qui annus est?

PGP Key 0x776DB663 = DD10 5C62 1E29 A385 9866 0853 CD38 E07A 776D B663
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFB5iFgFNNkhamc620RArBuAKCF/Y10NwVcibuOFZ3kDprIfDJuSgCfY8Wk
UGWTr/2MXAdOyz7iZFRQxPw=
=Hnqz
-END PGP SIGNATURE-

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]