Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: using split

by GrandFather (Saint)
on Jun 04, 2007 at 21:19 UTC ( #619231=note: print w/replies, xml ) Need Help??

in reply to using split

Note: it often helps you and us if you reduce your sample code to just demonstrate the bug. Most likely in the process you will solve the problem, but even if you don't you will understand it better. In either case you will remember the lesson better at the end of the day.

Your sample could have reduced to:

use strict; use warnings; my $line = "This|that"; my @cols = split(/|/, $line); print "@cols";


T h i s | t h a t

where I expected:

This that

I know what I mean. Why don't you? has a few other related tips that may help in the future.

Why no use strict by the way? Especially puzzling as there is a use warnings.


Note too that the 3 parameter open is much preferred over the two parameter open (see open) for lucidity and security.

if ($cols[11] = $target1)

looks wrong! Do you really intend to assign $target1 to $cols[11]? Note that the if is (effectively) testing the contents of $target1, not equality of $target1 and $cols[11].

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: using split
by naikonta (Curate) on Jun 05, 2007 at 02:21 UTC
    if ($cols[11] = $target1)

    looks wrong!

    Perhaps, but not really. One may need to assign a variable from another expression then test the truthness of that variable. What looks wrong if the variable is assigned to a literal value. This will issue a syntax warning (unless warnings are disabled):
    my $x; if ($x = 'a literal value') { # will issue warnings }
    Note that the if is (effectively) testing the contents of $target1, not equality of $target1 and $cols[11].
    While you are right about the equality part, but the if construct is effectively testing the whole expression, not just the RHS.

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

      I didn't say "is wrong", I said "looks wrong" (yes, I know you didn't say I said that either, but you seem to have reacted as though I did). It looks wrong to the extent that it should either be rewritten to "look right" or be commented. However the only appropriate comment effectively says "I did a dumb thing that needs an even dumber comment so that you know I'm being silly". Much better to rewrite it as:

      $x = $y; if ($x) { ... }

      which makes it quite clear that the assignment is intended and also quite clear what is being tested. Most countries don't tax code by the line so the cost of being clear is pretty low. ;)

      DWIM is Perl's answer to Gödel
Re^2: using split
by educated_foo (Vicar) on Jun 04, 2007 at 21:42 UTC
    I probably should just keep quiet, but... your sample response could have been reduced to:
    my $line = "This|that"; my @cols = split(/|/, $line); print "@cols";
    While the orthodoxy around here seems to be that everything must use strict and warnings, they're just line noise in code snippets.

      I sort of agree in the case of fairly trivial code like this. However the OP didn't use strict so the sample should, and when we say "always use strictures" then don't it weakens the argument a little.

      Note too that this is not a "code snippet", but a full working script that demonstrates the OP's issue. <toungfirmlyincheek>Surely any complete application should include strictures?</toungfirmlyincheek>

      DWIM is Perl's answer to Gödel
        I can understand adding a "use strict" when it would highlight the problem, but the presence/absence of strict was strictly irrelevant to the OP's problem. I personally use strict in modules (i.e. code used by other code) but not in scripts, and find the Perlmonks "use strict" dogmatism heavy-handed and annoying.

        And surely any "complete application" should have a test suite, too, but we shouldn't expect people to include those with their replies!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2021-04-17 12:24 GMT
Find Nodes?
    Voting Booth?

    No recent polls found