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

fellow monks,

perl -ane '$value = pop(@F); if ( $value == 0 ) { print "AutoSplit Off +"} else { print @F[0 .. 2] }'
Is it possible to Switch off the autosplit during the process, when I execute the code from commandline ?

Update: 1 Corrected a grammar mistake.

Replies are listed 'Best First'.
Re: How to switch off the autosplit ?
by kyle (Abbot) on Jan 25, 2007 at 04:25 UTC

    You could always do it manually.

    perl -ne 'BEGIN{$autosplit = 1} @F=split(" ") if $autosplit; $value = pop(@F); if ( $value == 0 ) { $autosplit = 0 } else { print @F[0 .. 2] }'

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to switch off the autosplit ?
by graff (Chancellor) on Jan 25, 2007 at 04:49 UTC
    I don't quite understand the point of your question. When you use "-an" on the command line, you still have access to each complete line of input, via $_, as well as having access to the whitespace-delimited tokens via @F. So, if you want some event in the data to trigger a switch from using @F to using just $_, you can still use "-a".

    But your example is unclear, because it looks like you just want to stop doing whatever you were doing when the first value on a line happens to equal 0 (so why not just exit at that point?) Anyway, here's another way to do it:

    perl -ane 'BEGIN{$auto=1} $auto=0 if($F[0]==0); if($auto){print @F[0.. +2]} else{ }'
    I would expect that you would normally be putting something in that "else" block involving $_ ...

    If it's a question of "optimizing" (saving perl from doing all that work on splitting lines), then avoiding "-a" as suggested in the first reply will be the way to go. But usually, optimization is not much of an issue in one-liners like this.