Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Greetings fellow monks,

I just finished an interesting coding problem for work. We have an online test-taking & grading system, and while doing some updates for it, I needed an algorithm that renders a numeric sequence in order by letter, regardless of the numbers within the sequence. For example:

5, 100, 2, 8, 40 ...becomes... B, E, A, C, D

After some tinkering, I came up with the following:

my @original_array = qw(5, 100, 2, 8, 40); my @sequence; my @ordered = sort @original_array; for (my $y = 0; $y < scalar @ordered; $y++) { for (my $x = 0; $x < scalar @ordered; $x++) { push @sequence, $x + 1 if ($ordered[$x] == $original_array[scalar @sequence]); } } my @letter_sequence = map { local $_ = $_; tr/1-9/a-h/; $_ } @sequence +;

I was wondering if any of you all had encountered such a problem in the past and if so if you have any code for it that's shorter than what I've posted. The above works, as far as I can tell, but I have a sneeking suspicion that there's a much better way to write it. (Oh, and the @original_array will never be bigger than 9 elements.) Thanks.

gryphon
code('Perl') || die;


In reply to Render numeric sequence as array of letters by gryphon

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found