in reply to How do I use a hash to store my data
So one solution is to just use the right part as the keys and the left as the values.
#!/bin/perl -w use strict; my %hash; while( <>) { # note: the left part should be just one character $hash{$2}= $1 if( m/^\s*(.)\s*::=\s*([^\s]*)\s*$/); } while( my($key, $value)= each %hash) { print "/$key/ ::= /$value/\n"; }
Now if you want the left part as the keys you want the values of the hash to be array refs, each array containing the various expressions. Using the hash is also a little more tricky. You can use perl -d (the Perl debugger) to figure out the structure of the hash while the program is running.
#!/bin/perl -w use strict; my %hash; while( <>) { my($key, $value)= m/^\s*(.)\s*::=\s*([^\s]*)\s*$/; $hash{$key} ||= []; # create an array ref if needed push @{$hash{$key}}, $value; # add to the array } while( my($key, $values)= each %hash) { # the values are in @{$values} print "/$key/ ::= /", join( '/', @{$values}), "/\n"; }
|
|---|