Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

split function

by rgoud (Initiate)
on Jan 27, 2011 at 01:54 UTC ( [id://884459]=perlquestion: print w/replies, xml ) Need Help??

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

Gurus, Could someone help to convert this to perl:
NEWTIME=`date |awk '{print $4}'|awk -F: '{print $1$2}'` <-This is work +ing fine echo $NEWTIME 2047 # VAR1=`echo $NEWTIME|cut -c1-2` <- here it is failing echo $VAR1 20 # VAR2=`echo $NEWTIME|cut -c3-4` <- here it is failing # echo $VAR2 47
Thanks in advance

Replies are listed 'Best First'.
Re: split function
by ikegami (Patriarch) on Jan 27, 2011 at 03:38 UTC

    I'm confused. What does it mean to translate code that fails to work?

    [ Please use <p> at the start of paragraphs and <c>...</c> around computer text (code, data, output). The latter will make your code readable without you having to do any escaping or formatting. ]

      maybe ...format string for date command is what you need ... I guess...

      date "+%H" date "+%M"

Re: split function
by Anonymous Monk on Jan 27, 2011 at 07:56 UTC
    #!/usr/bin/perl -- use strict; use warnings; use Time::Piece; my $t = localtime; print $t, "\n", $t->strftime("%a %b %d %H:%M:%S %Y"),"\n"; print $t->strftime('%H'),"\n"; print $t->strftime('%M'),"\n"; __END__ Wed Jan 26 23:57:47 2011 Wed Jan 26 23:57:47 2011 23 57
    See Time::Piece
      Thank you, is there a way of substracting 20 minutes from currentt time. I need to convert tht time format into like 01/26/2011 03:29:00 and pass to the database api but I need to substract 20 minutes from the current time and submit the date in that format.
        use DateTime qw( ); my $dt = DateTime->now( time_zone => 'local' ); $dt->subtract( minutes => 20 ); print($dt->strftime("%d/%m/%Y %H:%M:%S"), "\n");

        DateTime

        Sure, visit the Time::Piece documentation, and read all about it
      Thank you, this is great! Is there a way of printing current time in the format: 01/27/2011 20:30:00
        %m/%d/%Y %H:%M:%S
Re: split function
by rowdog (Curate) on Jan 27, 2011 at 03:23 UTC
    $ date|awk '{print $4}'|awk -F: '{print $1$2}'|cut -c3-4 19
    $ perl -e 'print( (localtime)[1], "\n" )' 19

    I don't really understand awk so I might be mistaken. If you're looking for something other than minutes, consult localtime.

Re: split function
by bart (Canon) on Jan 27, 2011 at 11:34 UTC
    If all you want is minutes and seconds of time of day, just look at localtime. You can easily extract minutes and seconds, as integers, from the return value in list context.

    In addition, if you want more elaborate formatting, you can also look at strftime in POSIX. You can just pipe the results from localtime into it, like this:

    use POSIX qw(strftime); my $format = "Current time minutes: %M, seconds: %S\n"; print strftime $format, localtime;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-03-28 16:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found