Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PERL Question

Options
  • 03-09-2006 4:54pm
    #1
    Closed Accounts Posts: 1,541 ✭✭✭


    Hi,

    Im reading from a database in PERL. I read a list of dates into @dates but they appear in the format yyyy/mm/dd . For example $dates[0] gives 2006/09/02 but I would like to convert it to 02/09/2006 or even better September 2 2006?

    Any ideas appreciated?:confused:


Comments

  • Registered Users Posts: 568 ✭✭✭phil


    The simple and easy way (straight forward and the principle can be used in most languages):
    foreach $date (@dates) {
        my ($year, $mon, $day) = split(/\//, $date);
        push(@newdates, "$day/$mon/$year");
    }
    

    Now you've got @newdates populated with the format you want.

    If you had to do this a number of times, it might be handy to write it in a function you can use with map:
    sub date_conv($) {
        my ($date) = shift;
        my ($year, $mon, $day) = split(/\//, $date);
    
        return "$day/$mon/$year";
    }
    

    This function takes ONE date and converts it into the format. You can then use ``map'' to convert the array like so:
    @newdates = map ( date_conv($_), @dates );
    

    Btw, in PERL to print out data structures, use Data::Dumper (tip given to me on another forum years ago).

    At the top of your script:
    use Data::Dumper;
    

    and then to print your data structure:
    print Dumper(@dates);
    

    Phil.


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    Thanks Phil.

    Never thought of using the split function :o Doh.

    Works great and thanks for the advice. :)


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    You might be able to use the DATE_FORMAT function in MySQL to get it to reformat the date during the query.

    To get text (e.g . September 4, 2006) you can use the strftime function. It is in the POSIX module. See the strftime manpage for more information.


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    OK thanks.SQL and PERL are very versatile. Its just a pity they don't produce webgraphs and charts etc.


  • Closed Accounts Posts: 169 ✭✭akari no ryu


    Urm.. there are probably libraries for it, if not it wouldn't be that hard to write your own using PERL's image magick libraries or GD libraries.


  • Advertisement
  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    finnpark wrote:
    Its just a pity they don't produce webgraphs and charts etc.
    Look at ImageMagick. There is a perl module that will allow you to process the graphics. Very cool.


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    :eek: PERL keeps amazing me. I have come across an open source program called Ploticus .

    Its free and you can write a batch file to run it - it is basically command line commands. Do you think that I would be able to produce graphs of similar quality using PERL instead of ploticus and in general how long would it take a decent programmer to learn using web tutorials etc? :)

    Cheers by the way


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    daymobrew wrote:
    Look at ImageMagick. There is a perl module that will allow you to process the graphics. Very cool.

    It may be tricky enough to do nice plots/graphs with this but I can still think of other things to use it for. Nice job.


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    finnpark wrote:
    It may be tricky enough to do nice plots/graphs with this but I can still think of other things to use it for. Nice job.
    Look at PHP/SWF Charts. It is very nice for charts.
    It spits out XML that the SWF movie reads. Obviously you can write XML with perl, via a module (e.g. XML::Simple) or manually with 'print' calls. I did the latter.


  • Closed Accounts Posts: 1,541 ✭✭✭finnpark


    daymobrew wrote:
    Look at PHP/SWF Charts. It is very nice for charts.
    It spits out XML that the SWF movie reads. Obviously you can write XML with perl, via a module (e.g. XML::Simple) or manually with 'print' calls. I did the latter.

    Very cool indeed Damo :cool:


  • Advertisement
Advertisement