in reply to Convert a string into a hash
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Convert a string into a hash
by vitoco (Hermit) on Aug 15, 2009 at 04:26 UTC | |
by Marshall (Canon) on Aug 15, 2009 at 07:05 UTC | |
by vitoco (Hermit) on Aug 15, 2009 at 16:29 UTC | |
by Marshall (Canon) on Aug 18, 2009 at 17:47 UTC | |
by vitoco (Hermit) on Aug 18, 2009 at 21:22 UTC | |
| |
by Marshall (Canon) on Aug 15, 2009 at 07:18 UTC |