Picking values from timestamp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sigurjon
    New Member
    • Jul 2010
    • 3

    Picking values from timestamp

    Dear sirs/madams

    I am not sure whether or not this question has already been answered, but I am completely new to perl and my problem is as follows:

    I have comma separated values in a text file, which I need to do some stuff with, and the data starts with a timestamp, i.e. on the form "2008-06-13 15:25:00" (the quote marks are part of the data). I need to fish the year, day, hour and minute from this timestamp as seperate integers. Does anyone have a solution how to do this?

    Best regards, Sigurjon
  • toolic
    Recognized Expert New Member
    • Sep 2009
    • 70

    #2
    Use a regular expression to extract the date and time into an array:

    Code:
    use strict; 
    use warnings;
    use Data::Dumper;
    
    my $s = "2008-06-13 15:25:00";
    my @tokens = $s =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
    print Dumper(\@tokens);
    
    __END__
    
    $VAR1 = [
              '2008',
              '06',
              '13',
              '15',
              '25',
              '00'
            ];

    Comment

    • sigurjon
      New Member
      • Jul 2010
      • 3

      #3
      Thanks

      Thank you for your post toolic.

      Now I have an array of six integers, each representing one part of the timestamp. Can I refer to them individually like this: @tokens[0] (the year) and @tokens[1] (the month)? If not, how would I get them individually?

      Best regards, Sigurjon

      Comment

      • toolic
        Recognized Expert New Member
        • Sep 2009
        • 70

        #4
        Code:
        $tokens[0]
        perlintro

        Comment

        • sigurjon
          New Member
          • Jul 2010
          • 3

          #5
          Thanks again

          Dear toolic.

          I tried this solution and it worked perfectly. Problem solved.

          Very many thanks to you for your advice!

          Best wishes, Sigurjon

          Comment

          Working...