> For example, given 100 numbers would guess that its a numerical array while mixed strings and numbers would return that the best data type is string.

depends on your definition of number, but I think this helps:

DB<160> @a=("1",0) => (1, 0) DB<161> if (grep { $_+0 ne $_ } @a) { "string" } else { "num" } => "num" DB<162> @a=("1 ",0,"") => ("1 ", 0, "") DB<163> if (grep { $_+0 ne $_ } @a) { "string" } else { "num" } => "string"

> I am thinking along the lines of what most statistical applications do when you load a CSV file and they try to guesstimate what data type each column is.

scalar grep returns a number, your free to check for thresholds.

DB<166> @a=("1 ",0,"") => ("1 ", 0, "") DB<167> scalar grep { $_+0 ne $_ } @a => 2 DB<168> (grep { $_+0 ne $_ } @a) > @a/2 => 1

update

you could also use Scalar::Util which is core

DB<186> use Scalar::Util qw/looks_like_number/ => 0 DB<187> grep { ! looks_like_number($_) } 0,"1","2 ", 3.14, " 4 ", 5e +55, "six" => "six"

please not that now strings like " 4 " are considered numbers!

As I said depends a lot on your definition... =)

Cheers Rolf

( addicted to the Perl Programming Language)


In reply to Re: Best guess for data type by LanX
in thread Best guess for data type by spiros

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.