in reply to Spliting letters in a string

Well, if you really need to have the variable named $a like you had in your question, you can use this:
$_ = "abcdefghijklmnopqrstuvwxyz"; map {${$_} = $_} split //; print $c; #prints letter 'c' to the screen


The 15 year old, freshman programmer,
Stephen Rawls

Replies are listed 'Best First'.
(bbfu) (soft-refs) Re(2): Spliting letters in a string
by bbfu (Curate) on May 08, 2001 at 02:15 UTC

    I really do believe that you already realize that this is dangerous and were just offering it as a "well, if that's really what you want to do then this is how you do it..." kind of thing.

    Continuing in that spirit, some suggestions...

    You should make sure that you don't conflict with use strict by doing a (block-scoped) no strict 'refs';. You might also want to local $_. I would also suggest avoiding using map in void context because it spends a lot of energy building a return list and actually tends to be slower than an equivalent for loop in many cases; and, IMHO, it doesn't really improve the legibility any.

    { no strict 'refs'; local $_ = "abcdefghijklmnopqrstuvwxyz"; # or $string $$_ = $_ for split //; } print $c; #prints letter 'c' to the screen

    Of course, these are all just suggestions. You're code was perfectly useable to begin with. :-)

    Update: Improved the wording a little. Removed unnecessary braces in deref in code. Changed no meaning. :-)

    bbfu
    Seasons don't fear The Reaper.
    Nor do the wind, the sun, and the rain.
    We can be like they are.

      >>I really do believe that you already realize that this is dangerous and were just offering it as a "well, if that's really what you want to do then this is how you do it..." kind of thing. >>

      Yep. I just want to stress, I don't recomend that the questioner actually use this, I just meant it as exactly what you said. The reason I didn't provide the additional info was the fear that the questioner would actually use it : )

      The 15 year old, freshman programmer,
      Stephen Rawls

        Well, if you're going to shoot yourself in the foot, you might as well do it elegantly. ;-)

        I also think it is a very bad idea for people to go around using soft references like that. That's why I posted my array suggestion below (and why I felt compelled to mention that it was a bad idea; out loud, as it were). Besides the danger of overwriting someone else's (or your own!) variables, there's the simple fact that, once you're done creating all those variables, you won't know which they were! :-)

        bbfu
        Seasons don't fear The Reaper.
        Nor do the wind, the sun, and the rain.
        We can be like they are.