Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

converting from php

by caterham7 (Initiate)
on Nov 20, 2005 at 17:50 UTC ( [id://510259]=perlquestion: print w/replies, xml ) Need Help??

caterham7 has asked for the wisdom of the Perl Monks concerning the following question:

Hello there

I am trying to change a php script into perl and have a couple of issues:

- what is the equivalent in perl to "foreach($the_days as $eachday){etc. " in perl?

- what is the equivalent in perl to "$the_days[$j] = strftime("%a, %d %b %Y", strtotime("+$jump days"));"

Many thanks

Edit: g0n - code tags & line breaks

Replies are listed 'Best First'.
Re: converting from php
by Corion (Patriarch) on Nov 20, 2005 at 17:59 UTC

    Have you tried searching or googling for your terms?

    Perl foreach gives me lots of pages with example usage of foreach. Perl strftime gives me lots of pages which point to the POSIX module.

    To make it more complete, here are the answers:

    foreach my $eachday (@the_days) { print $eachday; # "eachday" is a bad name }; use POSIX qw(strftime); print strftime('%Y ...', localtime)

    If you want to do date math, you are best off by using a module like Date::Calc or DateTime, or maybe it's already sufficient to just add 60*60*24 seconds to time, depending on your (untold) needs.

Re: converting from php
by cees (Curate) on Nov 21, 2005 at 03:33 UTC

    Here is a sample script that shows how to do this using the DateTime module. There are probably shorter ways of doing the same thing, but this shows you a small bit of the power of the DateTime module, which can do pretty much anything you ever want to do with dates.

    #!/usr/bin/perl use strict; use warnings; use DateTime; use DateTime::Format::Strptime; my $formatter = DateTime::Format::Strptime->new( pattern => '%a, %d %b %Y' ); my $today = DateTime->now( formatter => $formatter ); my @days = (); foreach my $day (1..14) { push @days, $today->clone->add( days => $day ); } print join("\n", @days), "\n";
Re: converting from php
by TedPride (Priest) on Nov 21, 2005 at 05:44 UTC
    The following produce equivalent results. $_ is the default if you don't specify a variable name.
    @the_days = ('whatever','values','go','here'); foreach $eachday (@the_days) { print "$eachday\n"; } for $eachday (@the_days) { print "$eachday\n"; } for (@the_days) { print "$_\n"; } print "$_\n" for @the_days;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://510259]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-19 22:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found