Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Is it possible to split a hash key?

by diotalevi (Canon)
on Jul 28, 2007 at 23:06 UTC ( [id://629340]=note: print w/replies, xml ) Need Help??


in reply to Is it possible to split a hash key?

You are too scared of proper escaping. The first parameter to split is always a regexp even if you write a string (unless that string happens to be a single space). You have written split( /./ , $log ) says to split between every non-newline character. Further, the performance of all non-5.10 regexp engines are reduced by writing your dots as [.] instead of the literal \.. Your regexp and pattern to split should both read /\./.

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^2: Is it possible to split a hash key?
by dbmathis (Scribe) on Jul 28, 2007 at 23:49 UTC
    That's right! I forgot to escape the reqex .

    Thanks for pointing that out.
Re^2: Is it possible to split a hash key?
by Anno (Deacon) on Jul 29, 2007 at 12:13 UTC
    Just a side note. You wrote:

    The first parameter to split is always a regexp even if you write a string (unless that string happens to be a single space).

    A single space also results in (the equivalent of) a regex in split. The difference is that the regex is not the string itself (/ /) but /\s+/. Specifying ' ' is an alternaive way of invoking the default which is to split on white space.

    Anno

      ... The difference is that the regex is not the string itself (/ /) but /\s+/

      ...not to forget the subtle, additional magic of ' ' vs. /\s+/ in the handling of leading whitespace. Compare

      my $s = " foo bar baz"; print join("|", split /\s+/, $s), "\n"; # prints |foo|bar|baz print join("|", split ' ', $s), "\n"; # prints foo|bar|baz
        ...not to forget the subtle, additional magic of ' ' vs. /\s+/ in the handling of leading whitespace.

        Oh right, that too. split() is too clever for its own good by half.

        Anno

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://629340]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-19 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found