The code below demonstrates that $x (scalar), @x (list), %x (hash) are different things.
Your instinct that anything with Perl using array indices is probably wrong, is correct.
#!/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
};
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 $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
};
Now again of course since this initialization, why would you need the scalar string at all?
my @tokens = qw (32 15 4 72 13 28 14);
my $i=0;
my %tokens = map {$_ => $i++}@tokens;
yields the same result as above.
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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.