How to sort a date array?

S

SaltyBall

Hi All,

Here we have a date array like this
@array=(2006-01-03, 2006-03-24, 2005-01-06);
when I use
sort(@array)
it did not sort correctly, the month and date was not sort, I think it
is the "-" make the sort fail, what is the correct way to sort this array?

thanks!
 
P

Paul Lalli

SaltyBall said:
Here we have a date array like this
@array=(2006-01-03, 2006-03-24, 2005-01-06);

You do understand that evaluates to:
@array = (2002, 1979, 1998);
right?
when I use
sort(@array)

You should get a warning telling you about a useless use of sort() in
void context. You are enabling warnings, right?
it did not sort correctly, the month and date was not sort, I think it
is the "-" make the sort fail, what is the correct way to sort this array?

You can start by posting a SHORT but COMPLETE script that demonstrates
the actual errors you're seeing. You would do well to read the Posting
Guidelines for this group, which contain the above and other good
advice.

my @array=('2006-01-03', '2006-03-24', '2005-01-06');
@array = sort @array;
print "@array\n";

This prints out:
2005-01-06 2006-01-03 2006-03-24

Paul Lalli
 
A

anno4000

SaltyBall said:
Hi All,

Here we have a date array like this
@array=(2006-01-03, 2006-03-24, 2005-01-06);

That looks a bit like Perl but it isn't. Please post Perl, it makes
it easier for us to help you.
when I use
sort(@array)
it did not sort correctly, the month and date was not sort, I think it
is the "-" make the sort fail, what is the correct way to sort this array?

Instead of just claiming it doesn't sort correctly you should have
provided a script that *shows* what it does. Like this

my @array=qw( 2006-01-03 2006-03-24 2005-01-06);
my @sorted = sort @array;
print "(@array) -> (@sorted)\n";

It is annoying that we have to write the code to check your claim. You
should have provided it.

For me the above code prints

(2006-01-03 2006-03-24 2005-01-06) -> (2005-01-06 2006-01-03 2006-03-24)

which looks fine to me.

Anno
 
D

David Squire

SaltyBall said:
Hi All,

Here we have a date array like this
@array=(2006-01-03, 2006-03-24, 2005-01-06);

Do you realize that this array contains

(2002, 1979, 1998)

????

I think you mean

my @array=('2006-01-03', '2006-03-24', '2005-01-06');
when I use
sort(@array)

Where did you store the result of this? As you should see from the perl
documentation, sort *returns* a sorted list, it does not sort an array
in place.
it did not sort correctly, the month and date was not sort, I think it
is the "-" make the sort fail, what is the correct way to sort this array?

Please include small, complete script illustrating your problem -
including the output and why it is not as you expect.

Always include:

use strict;
use warnings;

at the start of you script. In this case warnings would have told you
your problem.


DS
 
A

anno4000

Tad McClellan said:
Sure it is.

Perl has a subtraction operator.

Yes, I noticed and cancelled the posting but it looks like it escaped.
Propagation delays on Usenet aren't what they used to be...

Anno
 
S

SaltyBall

Thanks all of you helpful guys, It is my fault not to paste the code
clearly, after reading your code, I finally learn that sort(@array) will
not change the array, we should assign it to another array just like
your code "@sorted=sort @array;".

I am a newbie and I am too foo to be stuck with this simple error for hours.

Thanks all of you again
 
D

David Squire

SaltyBall wrote:

[top-posting corrected. Please don't do that]
Thanks all of you helpful guys, It is my fault not to paste the code
clearly,

You should read the posting guidelines for the group before posting
here. That would have told you about not top-posting too. The posting
guidelines are posted here twice weekly.
after reading your code, I finally learn that sort(@array) will
not change the array, we should assign it to another array just like
your code "@sorted=sort @array;".

You should have learnt that from reading the documentation for "sort",
not by posting here.

perldoc -f sort

Ask perl to help you before asking here.


DS
 
J

jl_post

SaltyBall said:
Here we have a date array like this
@array=(2006-01-03, 2006-03-24, 2005-01-06);
when I use
sort(@array)
it did not sort correctly, the month and date was not sort, I think it
is the "-" make the sort fail, what is the correct way to sort this array?


From a later posting, I see that you understand that sort(@array) is
not an "in-place" sort and will not modify the contents of @array.
However, I think you might have missed what other posters were trying
to tell you about the "-":

You seem to think it is part of a string, but Perl is treating it a
the subtraction operator. In other words, when you write:

2006-01-03

Perl thinks you meant:

2006 minus 1 minus 3 ==> which equals 2002.

To illustrate this, if you change the line of code:

@array=(2006-01-03, 2006-03-24, 2005-01-06);

to:

print join(' ', 2006-01-03, 2006-03-24, 2005-01-06);

you'll see that the output is not:

2006-01-03 2006-03-24 2005-01-06

as you thought, but rather:

2002 1979 1998

which is the result of performing the subtraction on the year values.

You probably meant to treat the values as strings and write:

@array=("2006-01-03", "2006-03-24", "2005-01-06");

and then later:

@array = sort(@array);

I also strongly recommend putting the following two lines near the
top of your script:

use strict;
use warnings;

You might not realize it, but those two lines can immediately catch
up to about 90% of the errors you make. And they'll tell you the exact
line number the errors are on without you needing to spend fifteen
minutes trying to hunt each bug down.

Seriously, I recommend either including those two "use" lines, or
else consider using another programming language altogether. Not using
those two lines allows for so many things to go wrong (that would have
otherwise been easily caught and reported) that it's usually not really
worth programming in Perl without them. (I feel that this is still
true whether you're a beginner or an advanced Perl programmer.)

I hope this helps!

-- Jean-Luc

perl -le "print(pack'B*','0'.unpack'B*',pack'w*',
5592691776,37562575106519616,25926642752,354130435682904)"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top