Hi bulrush,

Here are a couple of tests, and just for fun my own (horribly inefficient, benchmark included) implementation using Perl's magic string increment:

#!/usr/bin/env perl use warnings; use strict; sub excelpos2ltr { # by bulrush, https://perlmonks.pair.com/?node_id=1 +168549 my($oldcol)=@_; # Col starts at 0='A' my($procname,$numeric,$ltr,$num2,$outs); $procname="excelpos2ltr"; # Alt method 4. $numeric = $oldcol % 26; $ltr = chr(65 + $numeric); #$num2 = intval($oldcol / 26); # old line $num2 = int($oldcol / 26); if ($num2 > 0) { $outs=excelpos2ltr($num2 - 1) . $ltr; } else { $outs=$ltr; } return $outs; } sub colname { my $n = "A"; $n++ for 1..shift; return $n; } my %tests = ( 0=>"A", 1=>"B", 25=>"Z", 26=>"AA", 27=>"AB", 99=>"CV", 420=>"PE", 479=>"RL", 14557=>"UMX", 285075=>"PERL", ); use Test::More; plan tests => 2 * scalar keys %tests; is excelpos2ltr($_), $tests{$_}, "excelpos2ltr $_ => $tests{$_}" for sort {$a<=>$b} keys %tests; is colname($_), $tests{$_}, "colname $_ => $tests{$_}" for sort {$a<=>$b} keys %tests; done_testing; use Benchmark 'cmpthese'; cmpthese(-5, { # not neccesarily a fair benchmark due to random test data excelpos2ltr => sub { excelpos2ltr(rand(10000)) }, colname => sub { colname(rand(10000)) }, });

Regards,
-- Hauke D


In reply to Re: Get excel column letter from number by haukex
in thread Get excel column letter from number by bulrush

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.