[Kc] Perl Noob

Scott Kahler scottk at uclick.com
Wed Sep 19 08:40:27 PDT 2007

On 9/19/07 10:06 AM, "Emmanuel Mejias" <emmanuel.mejias at gmail.com> wrote:

> Thanks for the feedback guys! Just to answer some questions out there, I am
> familiar with the -w switch as well as strict. The class that I am taking is
> through Oreilly Media and they use the Learning Perl book from Oreilly except
> that the instructor has not covered warnings or strict yet. Where as in the
> Perl by Example book that I was using prior to the class mentioned this early.
> In this exercise the instructor was asking that I use the pre-defined
> environment variable hash that is discussed in this lesson, which is print
> %ENV; so that is why I created the script as a hash originally. I'm just
> confused as to why it only prints out 3 and skips every other one.

I think the main issue of confusion there is the difference between a hash
(%) and an array (@).  An array being a list a, b, c, d where a hash can be
though of as a label value pair 1 => apple, 2 => banana, 3 => grape.  So in
your original program where you write this:

%env = ('USER',

The way I would write what you are doing here is:

%env = ( ŒUSER¹ => ŒSHELL¹,
         ŒHOSTNAME¹ => ŒTERM¹,
         ŒHOME¹ => Œ¹):

My translation of what you are trying to do is as follows

use warnings;
use strict;

#--- Setup Hash
my %env = (
 'USER'     => '',
 'SHELL'    => '',
 'HOSTNAME' => '',
 'TERM'     => '',
 'HOME'     => ''

#--- Query environment variables in
#--- the actual %ENV hash and
#--- put the value in the bucket the
#--- label corresponds to in the %env hash
foreach $key ( sort keys %env) {
 $env{$key} = $ENV{$key};

#--- print out the labels and values
#-- in the %env hash
foreach $key ( sort keys %env) {
 print "$key $env{$key}\n";

Scott Kahler
Systems Engineer 
uclick, LLC (an Andrews McMeel Universal Company)
scottk at uclick.com
www.uclick.com www.gocomics.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/kc/attachments/20070919/cc48bd62/attachment.html 

More information about the kc mailing list