Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

split and @

by keesturam (Initiate)
on Jan 07, 2013 at 18:29 UTC ( #1012086=perlquestion: print w/ replies, xml ) Need Help??
keesturam has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, After a gap I am resuming to learn perl. Can you please help me why using @ with split doesn't give desired output.

$input = "ab,cd,efghi,jkl"; @input = split(/,/,$input); $len = @input; print "$input[$len-1]"; # splits properly into 4 $input = "ab,cd,efghi,jkl"; @input = split(/@/,$input); $len = @input; print "$input[$len-1]"; # splits into 2!

Is there anything special that I haven't read about using @?


Replies are listed 'Best First'.
Re: split and @
by davido (Archbishop) on Jan 07, 2013 at 18:40 UTC

    In your first test, you're splitting on 'comma', and your input string conveniently contains commas. So you get the desired behavior of @input receiving elements ab, cd, efghi, and jkl.

    In your second test, you're splitting on @, which is not present in your input string at all. Thus (contrary to what your #comment says), the string isn't being split; it doesn't contain anything that matches your split pattern. Consequently, you get the undesired behavior of @input receiving a single element, "ab,cd,efghi,jkl"

    If you're dealing with CSV files, or anything more complex than very simple un-quoted comma delemited text, Text::CSV will save you headaches and development time in the longrun.


Re: split and @
by AnomalousMonk (Canon) on Jan 07, 2013 at 21:28 UTC
    Is there anything special that I haven't read about using @?

    Others have pointed out the conceptual problem illustrated by the example code in the OP. One further point about  @ is that it is the sigil of arrays in Perl, e.g.  @array etc. Arrays interpolate into double-quoted strings and into regexes just as scalars do. If you had been trying to split on a string containing an '@' character that might have been mistaken by the compiler for an array name, it would have been necessary to escape the '@' character (see example below). The naked '@' in the  /@/ regex cannot be mistaken for an array name and so needs no escape.

    Try this code without the \ escape (and with warnings and strictures enabled! – see note below):

    >perl -wMstrict -le "my $str = 'XXX@okYYY'; my @pieces = split /\@ok/, $str; printf qq{'$_' } for @pieces; " 'XXX' 'YYY'

    NOTE: As you are learning Perl, it is highly advisable to enable warnings and strictures (warnings and strict) in your code. The diagnostics pragma is also often very useful to learners. The incantation
        use warnings;
        use strict;
        use diagnostics;
    can be wonderfully informative, and I and even quite experienced Perlers routinely enable warnings and strictures.

Re: split and @
by ww (Bishop) on Jan 07, 2013 at 18:54 UTC
    Perhaps you'll clarify what you need to read (beyond davido's reply above) if you explain where you got the idea reflected in the comment at Ln 009.

    As noted, splitting on anything that's NOT in the string results in passing the WHOLE string direct to the array.

    In any case, perldoc -f split may be a starting point ... or perldoc perlretut.

Re: split and @
by dbuckhal (Pilgrim) on Jan 07, 2013 at 19:34 UTC
    If you were trying to view the split content as a string with this line:

    print "$input[$len-1]"; # splits properly into 4

    You can just change it to simply:

    print "@input";

    The quotes around the array will give you a quick idea of how your splitting worked out.


    ab cd efghi jkl

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012086]
Approved by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2016-07-28 10:59 GMT
Find Nodes?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?

    Results (253 votes). Check out past polls.