in reply to Re: Re: Simple hash assignment...or is it?
in thread Simple hash assignment...or is it?

I have rewritten your code to make it a bit Perl like. The following code is equivalent to your code -
use strict; use Data::Dumper; my $network_ifs = `netstat -i | grep -Ev "lo|sit|link|Name" | cut -f1 +-d" " | uniq | sort -n`; my @tcp_utilization_metrics = split '\n', $network_ifs; my %tcp_utilization_adapter_names; foreach my $network_if (@tcp_utilization_metrics) { next if ! $network_if; # ignore empty lines if any my $if_name = $network_if; if ($network_if =~ /en/) { substr($network_if, 2, 0) = "t"; # insert 't' } elsif ($network_if =~ /tr/) { substr($network_if, 1, 2) = "ok"; # replace 'ok' } elsif ($network_if =~ /at/) { substr($network_if, 2, 0) = "m"; # insert 'm' } # insert hash entries $tcp_utilization_adapter_names{$if_name} = $network_if; } # investigate the hash foreach (keys %tcp_utilization_adapter_names) { print "$tcp_utilization_adapter_names{$_}\n"; }
Perhaps you could tell us what is your expected hash output. I highly suspect that you want the following instead in your code (swap the key-value in the hash) -
# insert hash entries $tcp_utilization_adapter_names{$network_if} = $if_name;

Replies are listed 'Best First'.
Re: Re: Re: Re: Simple hash assignment...or is it?
by arootbeer (Novice) on Nov 22, 2003 at 01:17 UTC

    Thanks...I'm sure you can tell I come from a C++/Java upbringing. The hash that Dumper displays is exactly what I want the hash to look like. The problem is that the keys (in @tcp_utilization_metrics) which are used to create the hash, don't seem to work to retrieve values from the hash. That's what has me so confused: both the hash and the array LOOK right, but something I don't know about ISN'T right.

      To print the values in a hash, use the keys keyword.
      my %hash = ( 'a' => 1, 'b' => 2, 'c' => 3); foreach (keys %hash) { print "$_ => $hash{$_}\n"; }
      Cheers. :-)

        To print the values in a hash, use the keys keyword.
         
        my %hash = ( 'a' => 1, 'b' => 2, 'c' => 3);
        
        foreach (keys %hash) {
          print "$_ => $hash{$_}\n";
        }
        
          Cheers. :-)
        
        In general, I wouldn't say that. "keys" is a standard Perl function (not a keyword), of which you can omit the () because there is a prototype known for it. Furthermore:

        • If you want the values of a hash in no particular order as a list, use the values function.
          my @value = values %hash;
        • If you want the keys of a hash in no particular order as a list, use the keys function.
          my @key = keys %hash;
        • If you want either the values or keys in a particular order, use sort() as well.
          my @value = sort values %hash; my @key = sort keys %hash; foreach (sort keys %hash) { print "$_ => $hash{$_}\n"; }
        • If you want to show the keys and possibly the associated values in no particular order, use each.
          while (my $key = each %hash) { print "$key\n"; } while (my ($key,$value) = each %hash) { print "$key => $value\n"; }
        The reason for using each() over keys(), is that keys() (and values()) build a list of the values. For large hashes, this list can get very big and cosume a lot of memory. You don't have that when you're using each(), as it will only fetch one key or one key/value pair from the hash at a time.

        Liz

Re: Re: Re: Re: Simple hash assignment...or is it?
by arootbeer (Novice) on Nov 22, 2003 at 01:22 UTC
    mpeppler figured it out for me...see below. Thanks for your help!