CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Sortable date conversions

 



PapaGeek
User

Aug 9, 2013, 6:15 AM

Post #1 of 4 (1295 views)
Sortable date conversions Can't Post

Iím trying to extract data from a web page into a hash, then display it in date order.

The data is supplied as May 9, 2013 and Jun 15, 2013. When sorted in alpha order, Jun comes before May.

I need to convert the dates into 2013.05.09 and 2013.06.15 so they will sort properly.

What is the proper perl code to do this translation?

Iíve searched the web and tried multiple ways, but none of them have worked so far.


hwnd
User

Aug 9, 2013, 6:35 AM

Post #2 of 4 (1291 views)
Re: [PapaGeek] Sortable date conversions [In reply to] Can't Post

What format is the data from the webpage, or what is the link for the webpage so we can see how the data is formatted? To extract the data into a hash from the web, use either 'LWP::Simple' or 'WWW::Mechanize'.

For parsing the dates, you can use a module like 'Time::Piece' to do this.


(This post was edited by hwnd on Aug 9, 2013, 6:40 AM)


PapaGeek
User

Aug 9, 2013, 7:55 AM

Post #3 of 4 (1281 views)
Re: [hwnd] Sortable date conversions [In reply to] Can't Post

Thank you for the reply

Iím working on a program to do stock market analysis. A typical link would be:

http://finance.yahoo.com/q/hp?s=DELL+Historical+Prices

Thank you for the pointer to Time::Piece. Unless there is a better answer, the following code does work:


Code
my $testDate = "Jun 7, 2013"; 

my $sortableTime = Time::Piece->strptime($testDate, "%b %d, %Y")->strftime("%Y.%m.%d");

print "$sortableTime\n";


It returns ď2013.06.07Ē which can then be used as the hash key with the adjusted close price, returned to the caller, and the hash can then be worked with in date order using a sorted key.

The data obtained from yahoo comes back in reverse time order, newest to oldest. When computing things like moving average, the data has to be looked at in standard time order, oldest to newest. Creating a hash in the called method, then sorting it in the calling program takes care of that.

Again, thanks for the reply.

If someone has a more "Perl-like" way for an old C C++ programmer to code things, I'd still like to here from you!


Laurent_R
Veteran / Moderator

Aug 9, 2013, 10:43 AM

Post #4 of 4 (1272 views)
Re: [PapaGeek] Sortable date conversions [In reply to] Can't Post

Using a module is a very perlish way to do that.

If you really want to do it yourself "by hand", it is not so easy. You need a %month hash to convert month names into numbers, something like this:

Code
0  HASH(0x8035a248) 
'Apr' => 04
'Feb' => 02
'Jan' => 01
'Jul' => 07
'Jun' => 06
'Mar' => 03
'May' => 05

(I was too lazy to go beyond July.)

Then you can use 2 successive regexes:

Code
$testDate = "Jun 7, 2013"; 
$testDate =~ s/(\w{3}) (\d\d?), (\d{4})/$3-$month{$1}-$2/; # testdate is now 2013-06-7, need to add a 0 before the 7
$testDate =~ s/-(\d)$/-0$1/; # now we have 2013-06-07

But this does not look very nice, does it? You could also split the date into three pieces and rework each element of the array and then reassemble them in the right order. Probably even uglier.

Using an appropriate module is better in my view.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives