Dear Sharan, > Can someone tell me how to convert an array to hash. > > Each array entry has a row of values > e.g. a(0) = ab cd ef; a(1) = mn de fg > > The hash array needs to be constructed with one of the element in the > array row as the key. > e.g. hash{ab} = cd ef - ab is a string in the array row > hash{mn} = de fg - mn is a string in the array row > > Can someone comment?
Here an example: #!/usr/bin/perl use strict; use warnings; # for debugging use Data::Dumper; my @array = ( 'ab cd ef', 'mn de fg', ); my %hash; for my $element (@array) { if ($element =~ m{ ^ # match the beginning of the row ( \w{2} ) # match two characters [ ] # match one space (.*) # match the rest of the row }mxs ) { $hash{$1} = $2; } else { warn "error: incorrectly formatted array element: $element"; } } print Dumper \%hash; Cheers, Matteo -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/