There is nothing wrong with either approach. A few comments:
- I would forget about do{}, seldom is that needed in code like this. If there is some need to restrict the scope of my $i, that points to some other problem in the overall code.
- I would use slice (@ranking)[-1] instead of using something like $ranking[$#ranking].
- I started $i at 1 to avoid any ambiguity about undef vs zero value.

Have fun! you are on your way with several ideas that produce correct results. All will run WAY fast enough for your application.

#!/usr/bin/perl -w use strict; my $ranking_string = "32,15,4,72,13,28,14"; my @ranking = split (/,/, $ranking_string); my $i =1; my %ranking_hash = map {$_ => $i++} @ranking; my $default_best_num = (@ranking)[-1]; while (my $list = <DATA>) { chomp $list; my $best_num = $default_best_num; foreach my $num (split /,/, $list) { next unless $ranking_hash{$num}; $best_num = $num if $ranking_hash{$num} < $ranking_hash{$best_num +}; } print "$best_num\n"; } #prints: #15 #15 #14 __DATA__ 4,13,15 4,50,15,13 50,60,70
The requirements for this app seem to be a moving target. I thought I'd demo a completely different approach for you. I'm not recommending this for your current requirements, but sounds like something may pop up soon that might need this. This shows "hey, 15 is the "best" one, but what is second best one", etc. This uses the very powerful sort functions of Perl.
#!/usr/bin/perl -w use strict; my $ranking_string = "32,15,4,72,13,28,14"; my @ranking = split (/,/, $ranking_string); my $i =1; my %ranking_hash = map {$_ => $i++} @ranking; my $default_best_num = (@ranking)[-1]; while (my $line = <DATA>) { chomp $line; my @ranking = sort by_min_ranking grep {$ranking_hash{$_}} (split /,/, $line); if (@ranking){print "@ranking - first one is \"best\"\n"} else {print "$default_best_num - default used\n"} } sub by_min_ranking { $ranking_hash{$a} <=> $ranking_hash{$b} } #prints: #15 4 13 - first one is "best" #15 4 13 - first one is "best" #14 - default used

In reply to Re^7: Convert a string into a hash by Marshall
in thread Convert a string into a hash by vitoco

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.