$Bill Luebkert wrote:
> Bowie Bailey wrote:
> > [EMAIL PROTECTED] wrote:
> > 
> > > Er, aren't hash keys interpreted as strings, even if they contain
> > > digits?
> > 
> > Perl does not have a strict distinction between string and numeric
> > data.  They are both considered scalars.  The difference comes in
> > how you use the scalar value. 
> > 
> > Try this:
> > 
> >     $a = "10";
> >     $b = "2";
> 
> You can get around this by using fixed length numerics if you for
> example want to intermix them with non-numeric strings and still have
> them sort: 

Yes, but the question wasn't about how to make it work, the question
was about the difference.  The post I originally replied to got it's
numbers by counting occurrences of words.

>       my $a = sprintf '%04u', 10;     # assuming 4 digits max
>       my $b = sprintf '%04u', 2;

Or just:

    my $a = "0010";
    my $b = "0002";

> Then you can sort them using string compares and mix them in with your
> other strings.  Let's not get into what happens when a string has
> alpha and numerics in it (unless of course they're fixed in position
> - eg: abcd0001).

You can do that as long as you don't do any numeric operations on
them.

> A prime example of comparing numbers as strings would be dates.  If
> you specify a date as YYYYMMDD (or DDMMYYYY), you can easily sort
> them with a string compare and is a good way to store dates if binary
> epoch time isn't an option.

Actually, this is a case where it doesn't matter which one you use.
The digits are fixed, so they sort the same either way.

You would need a string comparison if your format was YYYY-MM-DD.

But this is all just an interesting side note.  What it comes down to
(unless you have some special circumstances) is this:

- If your data is numeric, use numeric comparisons.
- If your data contains other characters, use string comparisons (and
  watch your formatting carefully).

-- 
Bowie
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to