<br><br>
<div><span class="gmail_quote">On 9/19/07, <b class="gmail_sendername">Andrew Moore</b> <<a href="mailto:amoore@mooresystems.com">amoore@mooresystems.com</a>> wrote:</span>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">On Wed, Sep 19, 2007 at 10:06:18AM -0500, Emmanuel Mejias wrote:<br>> In this exercise the instructor was asking that I use the pre-defined
<br>> environment variable hash that is discussed in this lesson, which is print<br>> %ENV; so that is why I created the script as a hash originally. I'm just<br>> confused as to why it only prints out 3 and skips every other one.
<br><br>%ENV is already defined to contain a hash of things like:<br><br> 'PWD' => '/home/amoore',<br> 'LANG' => 'en_US',<br> 'USER' => 'amoore',
<br><br>It's pretty much always defined when perl is running, so you don't<br>have to define it yourself or anything.<br><br>And so you can certainly print stuff out of it as it sounds like your<br>instructor asked you to.
<br><br>print "HOME is: $ENV{'HOME'}\n";<br><br>Or, you could walk through a list of terms to print out:<br><br>foreach my $key qw( HOME USER TERM ) {<br> print "$key is set to: $ENV{$key}\n";
<br>}<br><br>You can even define that list of keys in advance:<br><br>my @keys = qw( HOME USER TERM );<br>foreach my $key ( @keys ) {<br> print "$key is set to: $ENV{$key}\n";<br>}<br><br>Or, you could walk through each of the keys in the hash and print out
<br>the values in order to get them all (especially if you didn't know<br>what keys may be in there when you wrote the program):<br><br>foreach my $key ( keys %ENV ) {<br> print "$key is set to: $ENV{$key}\n";
<br>}<br><br>It appears that you were trying to create a list of keys, like: my<br>@keys = qw( HOME USER TERM ); but using a hash. That's probably not a<br>good use of a hash, as you're discovering.<br><br>Hope this helps a bit, please ask more and show us your code if not.
<br><br>-Andy<br><br>_______________________________________________<br>kc mailing list<br><a href="mailto:kc@pm.org">kc@pm.org</a><br><a href="http://mail.pm.org/mailman/listinfo/kc">http://mail.pm.org/mailman/listinfo/kc
</a><br></blockquote></div>
<div><br>That worked great, Andrew! Thanks! I added my sort and two extra lines so it does what it's suppose to do and prints out envirnoment variables that I was asking it to print. here is my end result:</div>
<div> </div>
<div>#!/usr/bin/perl -w</div>
<div> </div>
<div>@key = qw(SHELL USER LANG HOSTNAME TERM HOME);</div>
<div> </div>
<div> foreach my $key (sort(keys %ENV)){</div>
<div> foreach $check (@key){</div>
<div> if ($key eq $check){</div>
<div> print "$key: $ENV{$key}\n";</div>
<div> }</div>
<div> }</div>
<div>}</div>