Thanks to all to sent help on this ... it was in fact
that I was using a reference instead of setting what I
thought was a key - value pair. .... not enough sleep
I guess. :-)
--- Gisle Aas <[EMAIL PROTECTED]> wrote:
> "Brian Raven" <[EMAIL PROTECTED]> writes:
>
> > John V. Pataki wrote:
> > >
> > > Is there a function or slick way to
> operate/modify a
> > > key in hash?
> > >
> > > I don't mean the value of the key, I mean the
> key
> > > itself.
> > >
> > > For instance I want to run through all keys (as
> well
> > > all sub keys if hashes of hashes occurs) and
> replace
> > > spaces with underscores and set all to lower
> case.
> > >
> > > I could copy to a temp hash, undef the orginal,
> loop
> > > through all in the temp and create the new hash
> while performing my
> > > modifications ( i.e.
> > > ($KEY = $key) = s/ /\_/g;
> > > $myhash{$KEY} = $temp{key};
> > > )
> >
> > That's one way. Another way would be to delete the
> original key and
> > insert a new entry with the transformed key.
> >
> > >
> > > to the keys on the fly ---- but I wondered if
> there
> > > was a slicker, more perlish way to do this on
> the
> > > entire hash or all keys ... or something..
> >
> > I don't know if this could be called slicker (I
> haven't even tried it,
> > so it may have some syntax errors), but the
> following should work.
> >
> > my @keys = map {s/ /\_/g} keys %hash;
> > my @vals = values %hash;
> > %hash = ();
> > @[EMAIL PROTECTED] = @vals;
>
>
> I would probably try to do it with something like
> this:
>
> for my $k (keys %hash) {
> if ((my $n = $k) =~ s/ /_/g) {
> $hash{$n} = delete $hash{$k};
> }
> }
>
> This will be more efficient if not all keys need the
> modification.
>
> > I don't think there is anything really slicker,
> except perhaps not doing
> > it at all, i.e. couldn't you transform the keys
> before inserting into
> > the hash?
>
> That would also be my preferred solution.
>
> Regards,
> Gisle Aas,
> ActiveState
>
> _______________________________________________
> ActivePerl mailing list
> [EMAIL PROTECTED]
> To unsubscribe:
http://listserv.ActiveState.com/mailman/mysubs
=====
John V. Pataki
Logged in to my Yahoo Mail account on the web.
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs