in reply to A quit key when no chomp

Since you're going to have to rewrite the code to step through each entry individually, telling you this isn't cheating.

A common mistake for people learning Perl (even if they're experienced programmers) is to learn what those funny characters mean. You can't say:

if (@input eq 'q') {

- because the @ funny character indicates an array(see #1) .

So @input indicates the whole array - making it nonsensical to ask if the array is equal to anything (see #2). If you want to ask if a particular element of an array is equal to anything, you have to refer to that element explicitly, using the $ funny character. So, to test the first element of an array, we do somthing like this:

@foo = ('Bob', 'Tim', 'Bongo - The Jungle Avenger'); if ($foo[0] eq 'Bob') { #The $ indicates a single element #and [0] indicates which one print "Everything has gone as planned"; }


Cheers,
Erik

#1 - Well, not exactly true. It indicates a list out of a data structure, so it could be an array or and array slice or a hash slice.

#2 - Also not entirely true. The @input is in scalar context, so it does make sense to ask if it's equal to something . . .just not the sense we think it does.

Replies are listed 'Best First'.
Re: Re: A quit key when no chomp
by ellem (Hermit) on Mar 03, 2002 at 07:41 UTC
    Ahh... that makes sense. How can the array eq one thing or another it is a list!
    Still I won't know what the input is. And the input won't be chomped for output reasons.
    So my first order of business is to get the array scalar(?) and find the 'q\n'?
    No wonder Randal didn't write it this way in the book!
    --
    ellem@optonline.net
    There's more than one way to do it, just don't use my way.

      A thing to keep in mind is what chomp really does. chomp is a safe chop. I don't want to give it away, but reconsider whether or not it matters if you chomp the input when you know what character will be removed.

      Cheers,
      Erik
        I thought chomp takes "\n"

        And since I want output that resembles:
        One
        Two
        Red
        347

        I thought that leaving the input (un)chomp(ed) would be a good thing.
        --
        ellem@optonline.net
        There's more than one way to do it, just don't use my way.