Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Spliting letters in a string

by Jemts (Monk)
on May 08, 2001 at 00:49 UTC ( #78647=perlquestion: print w/ replies, xml ) Need Help??
Jemts has asked for the wisdom of the Perl Monks concerning the following question:

This will probably be simple but i just want to see an easy way to do it.
Lets say i have a string
$string="abcdefghijklmnop"
Now i want to splict each letter of the string into its own variable like :
$a="a"
$b="b"
$c="c"
... and so on

Jemts

"If you're traveling in a time machine, and you're eating corn on the cob, I don't think it's going to affect things one way or the other. But here's the point I'm trying to make: Corn on the cob is good, isn't it."

Comment on Spliting letters in a string
Re: Spliting letters in a string
by KM (Priest) on May 08, 2001 at 00:53 UTC
    I would do this with a hash:

    #!/usr/bin/perl -w my $str = "abcdefg"; my %hash; %hash = map {$_ => $_} split //, $str; print qq{$_ => $hash{$_}\n} for sort keys %hash;

    When run:

    # ./t.pl a => a b => b c => c d => d e => e f => f g => g

    Cheers,
    KM

(jeffa) Re: Spliting letters in a string
by jeffa (Chancellor) on May 08, 2001 at 00:54 UTC
    Instead of using single variables (and having to get nasty with eval and forsake strict), use a hash instead:
    my $str = join('', ('a'..'p')); my %hash = map { $_ => $_ } split('',$str); # silly example of using the hash print $hash{j} . $hash{e} . $hash{f} . $hash{f} . $hash{a} . "\n";

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
      my %hash; @hash{'a'..'z'} = 'a'..'z';
      Hash slices, my man, hash slices.

      ar0n ]

Re: Spliting letters in a string
by srawls (Friar) on May 08, 2001 at 01:58 UTC
    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

      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

(bbfu) Re: Spliting letters in a string
by bbfu (Curate) on May 08, 2001 at 02:04 UTC

    I'm going to guess that the variable names being the same thing as the letters they contained was only a coincidence (although, perhaps an intentional one) and that you really meant that you just wanted each letter of the string to be in a separate variable.

    In that case, you don't need to bother with a hash: just use an array. Like so:

    @letters = split //, $string;

    If you did use a hash, you'd have to use keys to find out what letters were in the string and would end up getting back an array anyway. Plus, the letters would be irrevocably out of order. That doesn't seem like a useful solution any way around.

    Of course, my solution counts each single whitespace and punctuation character as a letter, which may not be what you want. <update>But then, so do all the other solutions presented so far. ;-P</update> I still think it's closer to what you meant, though.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://78647]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-12-21 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (102 votes), past polls