Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
$ is atomic and % and @ are molecular - Perl

"When do I use a $, when do I use an @, and when do I use a % ?" That is a frequently asked question on a Perl course, where a delegate had dabbled with a bit of Perl ahead of time.

You use @ if you're referring to the whole of a list - in other words a complete collection. If you're a chemist, you may refer to methane as CH4 - and that's a molecule - a collection.

And you use $ if you're referring to a single scalar value rather than a colelction as a whole. It might be a quite separate scalar variable, or it might be an individual member of a list in which cas you'll put its index number (position) in the list in square brackets. The $ is a single value - an atom to the chemist.

Here's an example to show what I mean

  # Whole list with @
  @menu = ("Coffee","Muffin","Plate","Toast","Tea");
  print "@menu\n";
  
  # Individual member with $
  $menu[2]="Croissant";
  print "$menu[1]\n";
  
  # Whole list again - with changed individual member
  print "@menu\n";


And when we run that ...

  munchkin:ap1 grahamellis$ perl lz
  Coffee Muffin Plate Toast Tea
  Muffin
  Coffee Muffin Croissant Toast Tea
  munchkin:ap1 grahamellis$


There's a sample showing various examples [here] from yesterday's Perl course ... and there are older examples (which are included and documented in our training notes that accompany the courses [here] and [here].





So - what about the % character? That's used to access a hash. A list (which we saw above) is a collection of sequentially numbered scalars, from 0 up, whereas the members of a hash are (typically) named rather than numbered, and aren't in any particuar ordered sequence.

Here's a code sample like the one above - this time using a hash:

  # Whole hash with %
  %menu = (Bob => "Coffee",Brenda => "Muffin",Thomas => "Plate",
       Felicity => "Toast",Sasha => "Tea");
  print %menu,"\n";
  
  # Individual member with $
  $menu{Thomas}="Croissant";
  print "$menu{Brenda}\n";
  
  # Whole hash again - with changed individual member
  print %menu,"\n";


And running that:

  munchkin:ap1 grahamellis$ perl hz
  SashaTeaFelicityToastBobCoffeeThomasPlateBrendaMuffin
  Muffin
  SashaTeaFelicityToastBobCoffeeThomasCroissantBrendaMuffin
  munchkin:ap1 grahamellis$


With a hash, you can't simply loop through a series of index numbers to reference items by their position number - you'll normall use the keys function. There's an example here - again, we cover that in detail on our Perl courses.
(written 2011-08-20)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P211 - Perl - Hashes
  [240] Conventional restraints removed - (2005-03-09)
  [386] What is a callback? - (2005-07-22)
  [738] (Perl) Callbacks - what are they? - (2006-05-30)
  [930] -> , >= and => in Perl - (2006-11-18)
  [968] Perl - a list or a hash? - (2006-12-06)
  [1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
  [1705] Environment variables in Perl / use Env - (2008-07-11)
  [1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
  [1856] A few of my favourite things - (2008-10-26)
  [1917] Out of memory during array extend - Perl - (2008-12-02)
  [2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
  [2836] Perl - the duplicate key problem explained, and solutions offered - (2010-06-28)
  [2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
  [3072] Finding elements common to many lists / arrays - (2010-11-26)
  [3106] Buckets - (2010-12-26)
  [3451] Why would you want to use a Perl hash? - (2011-09-20)
  [3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)

P208 - Perl - Lists
  [28] Perl for breakfast - (2004-08-25)
  [140] Comparison Chart for Perl programmers - list functions - (2004-12-04)
  [230] Course sizes - beware of marketing statistics - (2005-02-27)
  [355] Context in Perl - (2005-06-22)
  [463] Splitting the difference - (2005-10-13)
  [560] The fencepost problem - (2006-01-10)
  [622] Queues and barrel rolls in Perl - (2006-02-24)
  [762] Huge data files - what happened earlier? - (2006-06-15)
  [773] Breaking bread - (2006-06-22)
  [928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
  [1304] Last elements in a Perl or Python list - (2007-08-16)
  [1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
  [1703] Perl ... adding to a list - end, middle, start - (2008-07-09)
  [1828] Perl - map to process every member of a list (array) - (2008-10-09)
  [1918] Perl Socket Programming Examples - (2008-12-02)
  [2067] Perl - lists do so much more than arrays - (2009-03-05)
  [2226] Revision / Summary of lists - Perl - (2009-06-10)
  [2295] The dog is not in trouble - (2009-07-17)
  [2484] Finding text and what surrounds it - contextual grep - (2009-10-30)
  [2813] Iterating over a Perl list and changing all items - (2010-06-15)
  [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
  [3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
  [3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
  [3870] Writing more maintainable Perl - naming fields from your data records - (2012-09-25)
  [3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
  [3939] Lots of ways of doing the same thing in Perl - list iteration - (2012-12-03)
  [4609] Mapping an array / list without a loop - how to do it in Perl 6 - (2016-01-03)


Back to
From fish, loaves and apples to money, plastic cards and BACS (Perl references explained)
Previous and next
or
Horse's mouth home
Forward to
Open Source Training Schedule - learn a programming language - in Autumn 2011 or 2012
Some other Articles
Buses on the Cambridge Guided Busway
Last chance this summer - Swindon and North Wiltshire to Weymouth by through train
That spec is a kingfisher ...
Open Source Training Schedule - learn a programming language - in Autumn 2011 or 2012
$ is atomic and % and @ are molecular - Perl
From fish, loaves and apples to money, plastic cards and BACS (Perl references explained)
Perl - making best use of the flexibility, but also using good coding standards
Does a for loop evaluate its end condition once, or on every iteration?
Tables as Objects in Lua - a gentle introduction to data driven programming
Parallel but not really parallel. Moving game characters. Coroutines in Lua.
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3400_-is ... -Perl.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb