in reply to Re^2: a use for hashes?
in thread a use for hashes?

Just replace the line that writes to the file with push @{ $relays{$id} }, $state;

Replies are listed 'Best First'.
Re^4: a use for hashes?
by Spooky (Beadle) on Feb 02, 2010 at 18:57 UTC
    Thanks ikegami! ..one last ?: if I have a relay followed by a number of states, e.g., relay1 on off set reset, how would I create an array or hash where the first element would contain the relay and the second would contain all of the various states - i.e., $ele[0] = relay1 $ele1 = on off set reset?
      Well, which one? Hash:
      my %relays; while (<>) { chomp; my ($id, $states) = split(' ', $_, 2); $relays{$id} = $states; }

      Array:

      my @relays; while (<>) { chomp; my ($id, $states) = split(' ', $_, 2); push @relays, [ $id, $states ]; }
        Thanks again ikegami - I like the options!
        Ikegami - Spooky here again. My relay exercise has expanded to include more fields: relay01 238 933 set relay02 238 934 9876536 2345.56 relay01 238 934 reset relay02 239 935 876555 23456.88 relay01 239 999 initiate relay03 240 877 899998 87698 relay03 241 888 98989898 3.34e-10 OK, there must be a way to do this in a loop. What I'm looking for is: relay01 238 238 239 relay01 933 934 999 relay01 set reset initiate relay02 238 239 relay02 934 935 relay02 9876536 876555 relay02 2345.56 23456.88 relay03 240 241 relay03 877 888 relay03 899998 98989898 relay03 87698 3.34e-10 (..not sure how Perl would handle an exponential value) You can see that each relay is listed with its seconds values, nanosecond values, a state, a "raw" value, and an engineering value. Any ideas?