Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > split is not convinient

Reply
Thread Tools

split is not convinient

 
 
Todd
Guest
Posts: n/a
 
      12-12-2007
Hi,


When I use split, there are serveral case frequently occured:

case1:

split # default to split ' ', $_

case2:

split /xxx/ # default to split /xxx/, $_

case3:

split ' ', expr # ????


I do think case3 is most frequently used, but why i've to type ' '
every time, why there are no simpler syntax to use in this case?


Thanks,
Todd
 
Reply With Quote
 
 
 
 
Randal L. Schwartz
Guest
Posts: n/a
 
      12-12-2007
>>>>> "Todd" == Todd <(E-Mail Removed)> writes:

Todd> case3:

Todd> split ' ', expr # ????


Todd> I do think case3 is most frequently used, but why i've to type ' '
Todd> every time, why there are no simpler syntax to use in this case?

Because split is positional, and it's only 4 characters to type that, and it's
not *that* common.

print "Just another Perl hacker,"; # the original

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      12-12-2007
Todd <(E-Mail Removed)> wrote in news:20d30e4a-8ded-4a3a-a99b-
http://www.velocityreviews.com/forums/(E-Mail Removed):

> case2:
>
> split /xxx/ # default to split /xxx/, $_
>
> case3:
>
> split ' ', expr # ????
>
>
> I do think case3 is most frequently used, but why i've to type ' '
> every time, why there are no simpler syntax to use in this case?



How would you decide whether the following is case 1 or case 2?

split 'hello';

The designers of the API made a choice that if only one argument is
specified, then it will be interpreted as the pattern.

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>

 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      12-12-2007
On Wed, 12 Dec 2007 08:07:57 -0800, Todd wrote:
> split ' ', expr # ????
>
>
> I do think case3 is most frequently used, but why i've to type ' ' every
> time, why there are no simpler syntax to use in this case?


AFAIK the perl built-in functions that have default arguments always only
allow you to remove the right-most argument(s).

That would make it a case of which of these in general are used the most
(written in full for clarity):

split /some pattern/,$_;

or

split ' ',$some_string;

And I can definitely tell you I use the former much more often than the
latter.

For instance:

while (<STDIN>) {
chomp;
my ($n,$v) = split /=/;
}

I probably use split /=/; already more often than I use split; split ' ',
$string and split/ /,$string; combined.

Joost.

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      12-12-2007
Todd wrote:
> When I use split, there are serveral case frequently occured:
> case1:
> split # default to split ' ', $_
>
> case2:
> split /xxx/ # default to split /xxx/, $_
>
> case3:
> split ' ', expr # ????
>
> I do think case3 is most frequently used, but why i've to type ' '
> every time, why there are no simpler syntax to use in this case?


One argument not being mentioned by others so far is that when a Perl
function allows omitting of an argument and thus substituting a default
argument it will be $_. Thisis the typical and expected Perl behaviour.
Interpreting a single argument as the text string rather than the search
pattern would be unexpected and contradictory to the typical behaviour of
all other Perl functions.

jue


 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      12-12-2007
Todd <(E-Mail Removed)> wrote:
> Hi,
>
> When I use split, there are serveral case frequently occured:
>
> case1:
>
> split # default to split ' ', $_
>
> case2:
>
> split /xxx/ # default to split /xxx/, $_
>
> case3:
>
> split ' ', expr # ????
>
> I do think case3 is most frequently used,


Not by me. When I use split on something other than $_, I almost
always use a pattern other than ' ', which I guess would be case4.

After a brief grep through some code, it looks like the order from most
used to least used would be case2 > case1 > case4 > case3

> but why i've to type ' '
> every time, why there are no simpler syntax to use in this case?



You could define a subroutine name splitd (d for default, as in ' ' being
the default pattern)

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      12-12-2007

Quoth "Jürgen Exner" <(E-Mail Removed)>:
>
> One argument not being mentioned by others so far is that when a Perl
> function allows omitting of an argument and thus substituting a default
> argument it will be $_. Thisis the typical and expected Perl behaviour.
> Interpreting a single argument as the text string rather than the search
> pattern would be unexpected and contradictory to the typical behaviour of
> all other Perl functions.


Uh huh... like shift, or readline, or the return value of split in
scalar context? There are many things I like about Perl; consistency is
not one of them.



Ben

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      12-12-2007
Ben Morrow wrote:
> Quoth "Jürgen Exner" <(E-Mail Removed)>:
>>
>> One argument not being mentioned by others so far is that when a Perl
>> function allows omitting of an argument and thus substituting a
>> default argument it will be $_. Thisis the typical and expected Perl
>> behaviour. Interpreting a single argument as the text string rather
>> than the search pattern would be unexpected and contradictory to the
>> typical behaviour of all other Perl functions.

>
> Uh huh... like shift,


Is not defined on scalars. When argument is omitted it shifts @_ which is as
close as possible to $_

> or readline,


The only argument is mandatory, you cannot omit it and thus cannot default
to $_.

> or the return value of split in scalar context?


Doesn't apply. We are talking about optional function/operator _arguments_
which when omitted will default to $_. Return values are not arguments.

jue


 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      12-12-2007

Quoth "Jürgen Exner" <(E-Mail Removed)>:
> Ben Morrow wrote:
>
> > or readline,

>
> The only argument is mandatory, you cannot omit it and thus cannot default
> to $_.


Yes. Sorry, that was a thinko; I meant <> and thought that readline had
the same default behaviour. To that list we can add -t, 1-arg open,
close (indeed, lots of the IO functions, including the mess that is
eof), substr, caller, bless, die/warn, exit, localtime, ...

I'm not saying any of these defaults are not sensible and useful, just
that they're far from consistent.

Ben

 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      12-12-2007
On Wed, 12 Dec 2007 23:00:28 +0000, Ben Morrow wrote:

> Quoth "Jürgen Exner" <(E-Mail Removed)>:
>> Ben Morrow wrote:
>>
>> > or readline,

>>
>> The only argument is mandatory, you cannot omit it and thus cannot
>> default to $_.

>
> Yes. Sorry, that was a thinko; I meant <> and thought that readline had
> the same default behaviour.


Don't forget while(<>) { ... }

> To that list we can add -t


which for /more-or-less/ obvious reasons defaults to _

> 1-arg open, close (indeed, lots of the IO functions, including the mess

that is
> eof), substr, caller, bless, die/warn, exit, localtime, ...
>
> I'm not saying any of these defaults are not sensible and useful, just
> that they're far from consistent.


You're definitely right there.

Joost.
 
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
a split is not a split Dumbell Computer Support 3 03-09-2009 10:45 PM
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM
How can I split database results with ExecuteReader and Split? needin4mation@gmail.com ASP .Net 2 05-05-2006 10:36 PM
split on '' (and another for split -1) trans. (T. Onoma) Ruby 10 12-28-2004 06:36 AM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments