Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Autosplit-Mode and $F[6] vs @F[6]

by strat (Canon)
on Feb 20, 2003 at 23:35 UTC ( #237273=perlquestion: print w/replies, xml ) Need Help??
strat has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

every time I use a perl-onliner with -ane and @F, I wonder why I can't access a single column of the input file with $F[6] (which contains undef) but have to use @F[6] and will get warned if I enable warnings about like the following way:

Scalar value @F[6] better written as $F[6] at -e line 1.

An example code may be:

perl -0012 -ane "print join(' => ',@F[8,6]),qq~\n~ if @F[6]=~/cmd\.exe +/" access.csv;
For input file, I used an apache access-log where the spaces were replaced by a tab as separator.

Do you know the reason for this strange behaviour?

Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8."

Replies are listed 'Best First'.
Re: Autosplit-Mode and $F[6] vs @F[6]
by xmath (Hermit) on Feb 21, 2003 at 00:57 UTC
    Hahaha, it took me a while to realize it...

    It's not perl, it's your shell! Commandline argument strings in double-quotes undergo variable substitution by the shell. :-D

    The solution:

    perl -0012 -ane 'print join(" => ",@F[8,6]),"\n" if $F[6]=~/cmd\.exe/' + access.csv
Re: Autosplit-Mode and $F[6] vs @F[6]
by Aristotle (Chancellor) on Feb 20, 2003 at 23:59 UTC
    I don't recall something like that and a quick test shows I cannot reproduce your problem. Are you sure @F really contains what you think it does and where you think it does?

    Makeshifts last the longest.

@F[6] vs. $F[6] - The Wrong Answer
by Elgon (Curate) on Feb 21, 2003 at 00:01 UTC

    Hi Strat,

    What is happening here is related to the way that Perl looks at individual values (scalars) and lists of values (arrays.) When you are referring to the whole list, stored in @F, say in a foreach loop...

    foreach $_(@F) # For each value in the list called @F { # Do stuff... }
    ...then you should use the @ prefix but when you want a single, discrete value within @F, such as $F[6], say in the following if clause...
    if ($F[6]) # If the seventh individual value in @F is true { # Do stuff }

    ...then you should use $F[6]. Essentially, if you're trying to access a single value then you'll want $, a list of values @ and a hash of key/value pairs %. Type "perldata" into the search box at the top of any perlmonks page and hit the search button. This should explain all you wish to know.


    Update : Apologies to all concerned, I completely misread the question in my eagerness to provide an answer. Please ignore the above. *Goes very red*

    "What this book tells me is that goose-stepping morons, such as yourself, should read books instead of burning them."
           - Dr. Jones Snr, Indiana Jones and the Last Crusade]

      If you read his post carefully you'll find he actually does know this.

      Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://237273]
Approved by Aristotle
NodeReaper practices with his stiletto

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2018-07-17 21:39 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (378 votes). Check out past polls.