The following code shows the very basic methods of assigning a value to a hash and accessing those scalar values by their keys.%hash; # a hash
@array; # an array
$hash{'key_string'} = 'some value';
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab07 Example 00 # Description: Very Basic hash syntax example #========================================================================== $fname{'Tucker'} = 'Mark'; $total{Jan} = 12.75; $total{Feb} = 42; print "Tucker's first name is $fname{'Tucker'}\n"; $month = 'Jan'; print "The total for $month is $total{$month}.\n"; exit; # DONE |
When executed, the script above produces the following output:
|
||||
[mark@iguana perl] ./lab07_0.pl Tucker's first name is Mark The total for Jan is 12.75. [mark@iguana perl] |
Above, the keys for the hash %total
are 'Jan' and
'Feb' with values of 12.75 and 42, respectively. The second
print statement shows the use of variable substitution in the
hash subscript.
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab07 Example 01 # Description: Assigning values to a hash as a list. #========================================================================== # assigning values to the hash as a list %station_temps = ( 'BTV', 32, '1V4', 28, 'MPV', 30, 'RUT', 35, 'CXX', 33, 'DDH', 40, 'JAY', 25 ); # sample output accessing the hash values $station = 'RUT'; print "Temp in $station is $station_temps{$station}\n"; # create an array from the hash @some_array = %station_temps; # list the values of the new array. $count = 0; foreach $k (@some_array) { print "element $count is $k\n"; $count++; } # some examples of what you cannot do print "Printing some_array as a hash $some_array{DDH} ??\n"; print "Printing station_temps as an array: $station_temps[3]\n"; exit; # DONE |
When executed, the script above produces the following output:
|
||||
[mark@iguana perl] ./lab07_1.pl Temp in RUT is 35 element 0 is JAY element 1 is 25 element 2 is DDH element 3 is 40 element 4 is CXX element 5 is 33 element 6 is RUT element 7 is 35 element 8 is BTV element 9 is 32 element 10 is MPV element 11 is 30 element 12 is 1V4 element 13 is 28 Use of uninitialized value in concatenation (.) or string at lab07_1.pl line 30. Printing some_array as a hash ?? Use of uninitialized value in concatenation (.) or string at lab07_1.pl line 31. Printing station_temps as an array: [mark@iguana perl] |
Notice that the array @some_array
can be assigned
the values of the hash on line 20 of the script above. When
listed as an array, the key values all have odd indexes and the
odd indexed elements contain the values of the hash. Also, you
cannot access a hash as an array nor can an array be accessed as
a hash. The two are NOT interchangeable.
Below shows another syntax for assigning key/values pairs in a hash
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab07 Example 02 # Description: Assigning values to a hash as a list. #========================================================================== # assigning values to the hash as a list %station_temps = ( 'BTV' => 32, '1V4' => 28, 'MPV' => 30, 'RUT' => 35, 'CXX' => 33, 'DDH' => 40, 'JAY' => 25 ); # sample output accessing the hash values $station = 'MPV'; print "Temp in $station is $station_temps{$station}\n"; exit; # DONE |
When executed, the script above produces the following output:
|
||||
[mark@iguana perl] ./lab07_2.pl Temp in MPV is 30 [mark@iguana perl] |
Values of a hash can be assigned based on external data. In the case below:
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab07 Example 03 # Description: Assigning values to a hash from external data #========================================================================== $datafile = '/mnt/homes/CLASSES/MET4720/LAB07/months.list'; # check and open the data file if( -r $datafile) { open(DF, "<$datafile") || die "cannot open file: $!"; }else{ print "Cannot read file: $datafile\n"; exit; } while($line = <DF>) { chomp($line); # split the line into values ($month,$days,$end_julian) = split(/,/, $line); # assign hash values with the month as the key $num_days{$month} = $days; $ending_julian{$month} = $end_julian; } close(DF); # Ask the user what month to use print "Enter a month (3 characters): "; chomp($enter_mon = <STDIN>); # user output print "$enter_mon has $num_days{$enter_mon} ending with julian day ". "$ending_julian{$enter_mon}\n"; exit; # DONE |
When executed, the script above produces the following output:
|
||||
[mark@iguana perl] cat months.list Jan,31,31 Feb,28,59 Mar,31,90 Apr,30,120 May,31,151 Jun,30,181 Jul,31,212 Aug,31,243 Sep,30,273 Oct,31,304 Nov,30,334 Dec,31,365 [mark@iguana perl] [mark@iguana perl] ./lab07_3.pl Enter a month (3 characters): Jul Jul has 31 ending with julian day 212 [mark@iguana perl] |
keys
function. The keys
function returns all the key
strings for the hash in a list (think array).
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab07 Example 04 # Description: working with hash keys #========================================================================== # assigning values to the hash as a list %station_temps = ( 'BTV' => 32, '1V4' => 28, 'MPV' => 30, 'RUT' => 35, 'CXX' => 33, 'DDH' => 40, 'JAY' => 25 ); # store all the key values in an array. @hash_keys = keys(%station_temps); print "@hash_keys\n"; # dump all the values and keys to the screen foreach $k (sort keys %station_temps) { print "$k -> $station_temps{$k}\n"; } exit; # DONE |
When executed, the script above produces the following output:
|
||||
[mark@iguana perl] ./lab07_4.pl JAY DDH CXX RUT BTV MPV 1V4 1V4 -> 28 BTV -> 32 CXX -> 33 DDH -> 40 JAY -> 25 MPV -> 30 RUT -> 35 [mark@iguana perl] |
env
command on Unix systems or with the
set
command under windows.
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab07 Example 05 # Description: Example of the %ENV hash #========================================================================== # dump all the values and keys to the screen foreach $k ( keys %ENV) { print "$k -> $ENV{$k}\n"; } exit; # DONE |
When executed, the script above produces the following output:
|
||||
[tuckerm@apollo LAB07] ./lab07_5.pl |head -5 SSH_CLIENT -> 216.114.179.103 33612 22 HOST -> apollo TSTRM_WARN -> /mnt/data/gempak/nwx/watch_warn/tstrm_warn TEXT_DATA -> /mnt/data/gempak/nwx DORADE_DIR -> /mnt/data/gempak/rawnids [tuckerm@apollo LAB07] [tuckerm@apollo LAB07] env |head -5 USER=tuckerm LOGNAME=tuckerm HOME=/mnt/homes/tuckerm PATH=/software/mcidas/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/usr/games:/usr/lib/j2re1.4.1_02/bin:.:/software/gempak/GEMPAK5.6k/bin/linux:/software/gempak/GEMPAK5.6k/bin/scripts:/opt/kde/bin:/software/vmware/bin MAIL=/var/mail/tuckerm [tuckerm@apollo LAB07] |
last updated: 18 Mar 2012 13:06