Reconsidering your requirements, and I will say that this is bizarre:#!/usr/bin/perl -w use strict; use Data::Dumper; my $tokens = "32,15,4,72,13,28,14"; my @tokens = (split/,/,$tokens); my %tokens = map {$_ => 1}@tokens; print Dumper (\%tokens); __END__ Prints: $VAR1 = { '4' => 1, '32' => 1, '28' => 1, '72' => 1, '13' => 1, '14' => 1, '15' => 1 };
Now again of course since this initialization, why would you need the scalar string at all?#!/usr/bin/perl -w use strict; use Data::Dumper; my $tokens = "32,15,4,72,13,28,14"; my @tokens = (split/,/,$tokens); my $i=0; my %tokens = map {$_ => $i++}@tokens; print Dumper (\%tokens); __END__ Prints: VAR1 = { '4' => 2, '32' => 0, '28' => 5, '72' => 3, '13' => 4, '14' => 6, '15' => 1 };
yields the same result as above.my @tokens = qw (32 15 4 72 13 28 14); my $i=0; my %tokens = map {$_ => $i++}@tokens;
Ok, I am going to go "crazy" here and ask why you want a hash in the first place? I am at a loss the see the usefulness of a hash here. Why do you think that you need it?
In reply to Re: Convert a string into a hash
by Marshall
in thread Convert a string into a hash
by vitoco
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |