bfilipow has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed Monks,

I have a problem with -F switch. I HAVE gone through one-liner perldocumentation.

Assume I want to write every second character from input. I wrote code which works fine:

ls -al | perl -lne '@F=split //; $i=0; @a = map {$i++%2 ? $_ : " "} @F; print @a;'

But my question is why the code below does not work? I.e. it produces blank output:

ls -al | perl -F// -lne '$i=0; @a = map {$i++%2 ? $_ : " "} @F; print @a;'

perl -h clearly states that:

-F/pattern/     split() pattern for -a switch (//'s are optional)

and -a is described as:

-a              autosplit mode with -n or -p (splits $_ into @F)

I use "perl, v5.8.3 built for x86_64-linux-thread-multi"

Thank you for your patience,
bfilipow-the-wisdom-seeker


Thank you again wise monks

Replies are listed 'Best First'.
Re: Perl oneliner problem with -F switch
by McDarren (Abbot) on Dec 02, 2005 at 09:37 UTC
    and -a is described as: -a autosplit mode with -n or -p (splits $_ into @F)

    yes, but you ommitted the -a switch from your 2nd one-liner.
    You didn't need it in the first one because you explicity declared @F

    The following works fine for me:

    ls -al | perl -F// -lane '$i=0; @a = map {$i++%2 ? $_ : " "} @F; print + @a;'

    Cheers,
    Darren :)

Re: Perl oneliner problem with -F switch
by Fang (Pilgrim) on Dec 02, 2005 at 09:37 UTC

    But my question is why the code below does not work? I.e. it produces blank output:

    ls -al | perl -F// -lne '$i=0; @a = map {$i++%2 ? $_ : " "} @F; print @a;'

    Probably because you actually forgot that -a switch you're also talking about.

    ls -al | perl -F// -alne '$i=0; @a = map {$i++%2 ? $_ : " "} @F; print @a;' works perfectly fine here.

Re: Perl oneliner problem with -F switch
by sauoq (Abbot) on Dec 02, 2005 at 09:38 UTC

    It works fine. You just forgot the -a switch in your command line.

    -sauoq
    "My two cents aren't worth a dime.";