Splitting a string with placeholders

B

Bryan

I have a string like this:
my $string = "A\tB\tC\t\t\t\t\t\t\n";

When I do:
my @array = split("\t", $string);

I get the following array:
@array = ["A", "B", "C"];

The placeholding '\t's are lost.

How can I keep these so that my array is like this:
@array = ["A", "B", "C", "", "", "", "", ""];

Thanks,
B
 
J

John W. Krahn

Bryan said:
I have a string like this:
my $string = "A\tB\tC\t\t\t\t\t\t\n";

When I do:
my @array = split("\t", $string);

I get the following array:
@array = ["A", "B", "C"];

The placeholding '\t's are lost.

How can I keep these so that my array is like this:
@array = ["A", "B", "C", "", "", "", "", ""];

my @array = split /\t/, $string, -1;



John
 
J

Jürgen Exner

Bryan said:
I have a string like this:
my $string = "A\tB\tC\t\t\t\t\t\t\n";

When I do:
my @array = split("\t", $string);

I get the following array:
@array = ["A", "B", "C"];

The placeholding '\t's are lost.

How can I keep these so that my array is like this:
@array = ["A", "B", "C", "", "", "", "", ""];

Did you check the documentation of split()?
<quote>
split /PATTERN/,EXPR,LIMIT
[...] If LIMIT is
unspecified or zero, trailing null fields are stripped [...]
If LIMIT is
negative, it is treated as if an arbitrarily large LIMIT had
been specified.
</quote>

jue
 
M

Mirco Wahab

Jürgen Exner said:
Bryan said:
my $string = "A\tB\tC\t\t\t\t\t\t\n";
Did you check the documentation of split()?
...
unspecified or zero, trailing null fields are stripped [...]
If LIMIT is
negative, it is treated as if an arbitrarily large LIMIT had
...

If I'm not mistaken, there's no 'trailing null field' here,
because it's an un-chomped string ...

Regards

M.
 
B

Bryan

John said:
Bryan said:
I have a string like this:
my $string = "A\tB\tC\t\t\t\t\t\t\n";

When I do:
my @array = split("\t", $string);

I get the following array:
@array = ["A", "B", "C"];

The placeholding '\t's are lost.

How can I keep these so that my array is like this:
@array = ["A", "B", "C", "", "", "", "", ""];

my @array = split /\t/, $string, -1;



John

Thanks John,

I need to omit the placeholder for the \n however. How do I do that?

BTW, where did you find the -1 thing? Its not in the perldoc -f split
info...

Cheers,
Bryan
 
B

Bryan

Bryan said:
John said:
Bryan said:
I have a string like this:
my $string = "A\tB\tC\t\t\t\t\t\t\n";

When I do:
my @array = split("\t", $string);

I get the following array:
@array = ["A", "B", "C"];

The placeholding '\t's are lost.

How can I keep these so that my array is like this:
@array = ["A", "B", "C", "", "", "", "", ""];

my @array = split /\t/, $string, -1;



John

Thanks John,

I need to omit the placeholder for the \n however. How do I do that?

BTW, where did you find the -1 thing? Its not in the perldoc -f split
info...

Cheers,
Bryan

Oh duh. Found it.
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top