in reply to create hash with map

In Addition to ikegamis solution, you
might use:
# |hash key| [hash val: array(comma sep values)] %cp_samp = map{ (split ':')[0], [split ',', (split ':')[1] ] }@ex_ar +ray; # display result with: print map "$_ => array [ @{ $cp_samp{$_} } ]\n", keys %cp_samp;
Regards
M.

Replies are listed 'Best First'.
Re^2: create hash with map
by ikegami (Patriarch) on Sep 24, 2007 at 15:39 UTC

    That gives

    Use of uninitialized value in split

    Fix:

    %cp = map{ (split /:/)[0], [split /,/, ((split /:/), '')[1] ] } @ex;

    Note that using a string as the first argument of split is misleading, since it expects a regexp. You might as well provide a regexp directly.


       Use of uninitialized value in split

      Depending on the input data (if inconsistent)
      this might happen but doesnt't do any harm
      to the whole thing

      In order to sanitize the stuff, one has to check
      for the existence of the second split field after
      the ':'
      Just by substitution of (split...)[1] with (split ...)[-1]
      a workaroud is found
      %cp_samp = map{ (split ':')[0],  [split ',', (split ':')[-1] ] }@ex_array;
      (corrected, see below ... sorry)

      or even

      %cp_samp = map{ (split /:/)[0], [split /,/,  ( (split /:/)[1] || '')] }@ex_array;

      But this is sth. I'd like the OP to have figured out ...

      Regards
      M.

        The OP didn't specify what he wanted for "sample3", but I doubt it's

        $VAR1 = { 'sample3' => [ 'sample3' ] };

        Your updated solution fails for "sample:0", which seems to be a likely input given the samples provided by the OP.

        But this is sth. I'd like the OP to have figured out ...

        Given your three failed attempts, it could very well be out of the reach of the OP.

        A reply falls below the community's threshold of quality. You may see it by logging in.