Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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 @?

Cheers!!

Comment on split and @
Download Code
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.


    Dave

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 (Monk) 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.

    ---output---

    ab cd efghi jkl
Re: split and @
by AnomalousMonk (Abbot) 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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2014-09-20 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (159 votes), past polls