Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Splitting a string with placeholders

Reply
Thread Tools

Splitting a string with placeholders

 
 
Bryan
Guest
Posts: n/a
 
      04-13-2007
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
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      04-13-2007
Bryan wrote:
> 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
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-13-2007
Bryan wrote:
> 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


 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      04-13-2007
Jürgen Exner wrote:
> Bryan wrote:
>> 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.

 
Reply With Quote
 
Bryan
Guest
Posts: n/a
 
      04-13-2007
John W. Krahn wrote:
> Bryan wrote:
>> 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
 
Reply With Quote
 
Bryan
Guest
Posts: n/a
 
      04-13-2007
Bryan wrote:
> John W. Krahn wrote:
>> Bryan wrote:
>>> 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.

 
Reply With Quote
 
Scott Bryce
Guest
Posts: n/a
 
      04-13-2007
Bryan wrote:

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


Chomp $string before you split it.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ignoring some placeholders in string formatting Michal Ludvig Python 1 02-11-2010 02:11 AM
Splitting Strings in a map<string, string> byte8bits@gmail.com C++ 3 04-15-2008 06:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments