Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Convert a string into a hash

by Marshall (Prior)
on Aug 15, 2009 at 00:52 UTC ( #788794=note: print w/ replies, xml ) Need Help??


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?


Comment on Re: Convert a string into a hash
Select or Download Code
Replies are listed 'Best First'.
Re^2: Convert a string into a hash
by vitoco (Friar) 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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (15)
As of 2015-07-30 19:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls