Split for non-Tab String

T

TS

Hi,

If I got a string in non tab format like below, how could i split it
into three arrays ($name, $height, $weight) using the split function.
Thanks

$input = 'Amy 170cm 50kg'
 
U

usenet

TS said:
If I got a string in non tab format like below, how could i split it
into three arrays ($name, $height, $weight) using the split function.

$input = 'Amy 170cm 50kg'

Normally I would simply respond to a queston like this with an answer
like this:

perldoc -f split

except that, in this case, I don't think the docs are especially clear
(because, to me, the term "splits on whitespace" doesn't necessairly
mean one single whitespace, because I usually think of the term
'whitespace' as (possibly) plural), so I'll just tell you:

split (/\s+/, $input);

(although I still recommend the perdoc for further reading.)

Cheers!
 
U

usenet

TS said:
If I got a string in non tab format like below, how could i split it
into three arrays

I just noticed you said "arrays". I presume you meant either "three
scalars" or "one array."

BTW, split() in Perl has nothing to do with tabs (unless you force it
to). It defaults to a single whitespace as a record delimiter.
 
A

A. Sinan Unur

(e-mail address removed) wrote in
Normally I would simply respond to a queston like this with an answer
like this:

perldoc -f split

Which you still should.
except that, in this case, I don't think the docs are especially clear
(because, to me, the term "splits on whitespace" doesn't necessairly
mean one single whitespace,

The only place where 'splits on whitespace is mentioned is:

If EXPR is omitted, splits the $_ string. If PATTERN is also
omitted, splits on whitespace (after skipping any leading
whitespace).

So:

#!/usr/bin/perl

use strict;
use warnings;

{
local *_;
$_ = 'Amy 170cm 50kg';
my @output = split;
print join("\n", @output), "\n";
}

__END__

D:\Home\asu1\UseNet\clpmisc> tt
Amy
170cm
50kg
 
A

axel

Normally I would simply respond to a queston like this with an answer
like this:
perldoc -f split
except that, in this case, I don't think the docs are especially clear
(because, to me, the term "splits on whitespace" doesn't necessairly
mean one single whitespace, because I usually think of the term
'whitespace' as (possibly) plural), so I'll just tell you:
split (/\s+/, $input);

But there is a specific method of specifying 'split on whitespace', viz.

split ' ', $input;

Axel
 
A

A. Sinan Unur

(e-mail address removed) wrote in


Which you still should.

And, apparently, I should make sure to read the whole thing :)

....
The only place where 'splits on whitespace is mentioned is:

Actually, "splits on whitespace" is mentioned in two places.

Sinan
 
A

Anno Siegel

I just noticed you said "arrays". I presume you meant either "three
scalars" or "one array."

BTW, split() in Perl has nothing to do with tabs (unless you force it
to). It defaults to a single whitespace as a record delimiter.

No, it defaults to /\s+/.

split ' ', $string;

may look like it splits on single blanks, but it's just another way of
invoking the default.

Anno
 
U

usenet

Anno said:
No, it defaults to /\s+/.
split ' ', $string;

may look like it splits on single blanks, but it's just another way of
invoking the default.

No kidding? I never knew that!
 
A

Anno Siegel

No kidding? I never knew that!

The split function is full of nooks and crannies. It is one of the
most frequently used Perl functions, but I'm sure I'm not the only
one who has to look it up in perlfunc regularly.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top