http://www.perlmonks.org?node_id=788794


in reply to Convert a string into a hash

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?

Replies are listed 'Best First'.
Re^2: Convert a string into a hash
by vitoco (Hermit) on Aug 15, 2009 at 04:26 UTC
    Ok, I am going to go "crazy" here and ask why you want a hash in the first place?

    I just want to iterate the elements of another list of tokens, but in the same order them appear in the main list of tokens. Simplified:

    my $tokens = "32,15,4,72,13,28,14"; my %code = do { my $i = 0; map { $_ => $i++ } split /,/, $tokens }; my $list = "4,13,15"; my $ok; for my $token (sort { $code{$a} <=> $code{$b} } split /,/, $list) { print "$token "; # more code with $token... last if $ok; } __END__ 15 4 13

    Obviously, both $tokens and $list are dynamically loaded from somewhere else...

      This is a straight-forward implementation.
      Post again if I didn't get it right....
      #!/usr/bin/perl -w use strict; my $tokens = "32,15,4,72,13,28,14"; my @tokens = split (/,/, $tokens); my $list = "4,13,15"; my @list = split (/,/,$list); my %list = map {$_ => 1}@list; my @ordered_nums = grep{$list{$_}}@tokens; print "@ordered_nums\n"; __END__ PRINTS: 15 4 13

        That was the other aproach I thought, but the requirement changed to pick only the best token from $list, which is loaded many times based on some external events, while %code is initialized once.

        Based on the previous, I decided to keep that part, and now I want to rewrite:

        my $best = 14; # actually, last value from $tokens. for my $b (split /,/, $list) { $best = $b if $code{$b} < $code{$best}; }

        Update: syntax error, extra ")"...

      Ooops, mess up and double post...sorry.... This is a straight-forward implementation.
      Post again if I didn't get it right....
      #!/usr/bin/perl -w use strict; my $tokens = "32,15,4,72,13,28,14"; my @tokens = split (/,/, $tokens); my $list = "4,13,15"; my @list = split (/,/,$list); my %list = map {$_ => 1}@list; my @ordered_nums = grep{$list{$_}}@tokens; print "@ordered_nums\n"; __END__ PRINTS: 15 4 13
      Update: I would perhaps replace "list" with "list_order". Matter of choice.