$foo - What is happening here?

Discussion in 'Scripting & Programming' started by dee1810, Oct 3, 2007.

  1. dee1810

    dee1810 Byte Poster

    142
    2
    29
    Hi everyone

    Now I've lost the will to live!!

    Does anyone recognise the importance of foo here? Please explain.

    @list1 = ("alpha", "beta", "gamma");
    @list2 = ("one", "two", "three");
    $key = $list1[$i];
    $value = $list2[$i];

    $foo {$key} = $value;

    The subject is hashes and keys. (yeah, I'm studying Perl Fundamentals :eek:)
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  2. UCHEEKYMONKEY
    Honorary Member

    UCHEEKYMONKEY R.I.P - gone but never forgotten. Gold Member

    4,140
    58
    214
    Sorry the only foo I know is the Foo Fighters:biggrin8)

    eeck!!! :ohmyI just realise that's perl.

    I will have to get out my notes for that one:study
     
    Certifications: Comptia A+
    WIP: Comptia N+
  3. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    You are missing some code here I suspect. Perhaps there is a loop on $i around the last 3 lines?

    foo here is a hash. What seems to be happening here is you are initializing the hash %foo so that each item with the key from list 1 contains the value from list 2.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  4. dee1810

    dee1810 Byte Poster

    142
    2
    29
    Hi Thanks for answering me...

    I've just noticed this in my notes..."To access specific elements in a hash, you enter the name of the hash as a scalar variable, followed by the key for the element you want to access in braces"

    Therefore foo actually means nothing except its a variable name. :twisted:

    I think foo is actually something more complex, which is why it's confused the hell out of me!!!!!
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  5. dee1810

    dee1810 Byte Poster

    142
    2
    29
    Hi Harry

    The example in my course syllabus has a "lone" brace at the end. It's all on it's own with no opening brace. }

    What a poor example.

    Harry - All of the syntax that is here is on the screen.
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  6. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    I'm not sure quite what you mean by 'means'! :biggrin

    foo is a name - nothing more. Just as $key is a name.

    The *type* of foo is a hash. The type of key is a scalar.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  7. UCHEEKYMONKEY
    Honorary Member

    UCHEEKYMONKEY R.I.P - gone but never forgotten. Gold Member

    4,140
    58
    214
    Harry comes to the rescue:biggrin:clap:thumblefthen again if it's just the termology have look at Wiki:-

    JOSM
     
    Certifications: Comptia A+
    WIP: Comptia N+
  8. dee1810

    dee1810 Byte Poster

    142
    2
    29
    Hi Both

    Thanks again.

    It isn't the terminology that I needed. The variable foo was just included in this example, my interpreter can't read it, and so I was just confused. It hasn't been mentioned before in this course, and in this subject (hashes) it isn't mentioned again.
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  9. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    Hm - I'd expect something like:
    Code:
    for ($i = 0; $i < 3; $i++)  {
        $key = $list1[$i];
        $value = $list2[$i];
    
        $foo {$key} = $value;
    }
    
    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  10. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    The code as presented should have been OK in a perl interpreter as long as 'strict' and 'warnings' aren't on. If they *are* on then it would have failed for other reasons.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  11. stuPeas

    stuPeas Megabyte Poster

    774
    12
    76
    foo is just a name they use for variable. I think it has some comedic value in the Perl community but I'm not sure what. The variable may have easily been called anything else!!
     
    Certifications: C&G Electronic, CIW Associate (v5).
    WIP: CIW (Website Design Manager)
  12. dee1810

    dee1810 Byte Poster

    142
    2
    29
    Well, Harry...

    I see exactly what you mean... there is no loop in the example at all.

    No for loop is initialized, controlled or incremented.
    just....

    @list1 = ("alpha", "beta", "gamma");
    @list2 = ("one", "two", "three");
    $key = $list1[$i];
    $value = $list2[$i];

    $foo {$key} = $value;

    }


    Of course, I understand that the end brace HAS to be wrong, in the absence of an opening brace. This is why I didn't post it in my question. It's just a very bad example to show someone who is trying to understand Perl.
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  13. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  14. dee1810

    dee1810 Byte Poster

    142
    2
    29
    :confused3

    Yes, I'd just read that article when I posted my question. Its the reason that I lost the will to live.
    Then my course materials "throws" it in as a variable name, and stuffs me up completely.
    I'm frantically looking for a reference in my notes, and can't find it...
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  15. stuPeas

    stuPeas Megabyte Poster

    774
    12
    76
    As is, this code would not work; Not because of anything to do with $foo, but because $i has not been given a value. This means that $list1 will return a value of "undefined" and therefore would assign "undefined as the value of $key. This is the same with $list2 and $value.

    This means that in the last line ($foo {$key} = $value), what it is saying is: Assign the value "undefined" as the value for the corresponding key "undefined" in the hash %foo.

    Watch for more mistakes to come in the course manual Dee. One such mistake is where the value in an array is being accessed with something like @array[2], instead of $array[2].
     
    Certifications: C&G Electronic, CIW Associate (v5).
    WIP: CIW (Website Design Manager)
  16. dee1810

    dee1810 Byte Poster

    142
    2
    29
    Ok.. will do.
    Of all the CIW modules that they could have done the odd typo, I wish it didn't have to be this one. Mistakes in the course manual, really cheese me off!
     
    Certifications: Foundations, Site Designer & JavaScript
    WIP: Server Admin, and Perl
  17. ffreeloader

    ffreeloader Terabyte Poster

    3,661
    106
    167
    You think that's bad? I bought a book on T-SQL once in which the author deliberately put unworkable code samples so the reader would have to troubleshoot his code to get it to work. The only problem was he didn't say he had done that until the last chapter of the book. To say I was little bent out of shape after having to troubleshoot code all the way through the book when the book was my first introduction to any programming/scripting language, as well as to T-SQL, is an understatement. If an author is going to deliberately put bad code in a book he ought to announce it in the very first chapter, on the very first page, so people know what's coming and don't pull their hair out trying to figure out what is going on with the code samples.

    You should see me now. All I have now is a fringe of hair around the sides of my skull after reading that book. :twisted:

    Well, that's about all the hair I had before I read the book, but it makes a good story.... :biggrin
     
    Certifications: MCSE, MCDBA, CCNA, A+
    WIP: LPIC 1

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.