473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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
Jul 5 '10 #1

✓ answered by toolic

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

Expand|Select|Wrap|Line Numbers
  1. use strict; 
  2. use warnings;
  3. use Data::Dumper;
  4.  
  5. my $s = "2008-06-13 15:25:00";
  6. my @tokens = $s =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
  7. print Dumper(\@tokens);
  8.  
  9. __END__
  10.  
  11. $VAR1 = [
  12.           '2008',
  13.           '06',
  14.           '13',
  15.           '15',
  16.           '25',
  17.           '00'
  18.         ];

4 2265
toolic
70 Expert
Use a regular expression to extract the date and time into an array:

Expand|Select|Wrap|Line Numbers
  1. use strict; 
  2. use warnings;
  3. use Data::Dumper;
  4.  
  5. my $s = "2008-06-13 15:25:00";
  6. my @tokens = $s =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
  7. print Dumper(\@tokens);
  8.  
  9. __END__
  10.  
  11. $VAR1 = [
  12.           '2008',
  13.           '06',
  14.           '13',
  15.           '15',
  16.           '25',
  17.           '00'
  18.         ];
Jul 5 '10 #2
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
Jul 5 '10 #3
toolic
70 Expert
Expand|Select|Wrap|Line Numbers
  1. $tokens[0]
perlintro
Jul 5 '10 #4
Dear toolic.

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

Very many thanks to you for your advice!

Best wishes, Sigurjon
Jul 12 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: rong.guo | last post by:
Hello Group, I am having a really weird problem... Can anyone tell the difference between Query 1 and Query 2 below? Why Query 2 excludes '2/28/2005'? Many thanks! create table a...
2
by: Alex | last post by:
Hi all, Got a small problem here. I have a number of web caches here that generate loads and loads of log files and instead of keeping them for analysis I want to write them to a db/2...
16
by: Brian Tkatch | last post by:
I have a PROCEDURE that test other PROCEDUREs. Timings_Call ------------------ Id Group Name Text Timings_Log
4
by: mghale | last post by:
I have a question that I'm hoping has an easy answer. I'm working in DB2 V8.2 on AIX 5.3 I have a timestamp column (i.e. 4/26/2006 1:02:42.000000 PM) that I want to return in a report from...
7
by: JJ | last post by:
How do I set one field to have the updated timestamp, and another to have the created timestamp? I want to do this directly from code generated from DB Designer if possible?! JJ
2
by: klaus.schu | last post by:
I just receive an error during my CLI ODBC insert statement: INSERT INTO (.....) VALUES (?, ?, ?, TIMESTAMP( CAST(? AS VARCHAR(10)),'00:00:00'), ?, ?, ?, ?)"; the ? in the CAST( ) Statement is...
5
by: Hemant Shah | last post by:
Folks, How can I convert date/time/timestamp to an integer? According to UDB 8.1 docs that I have, INTEGER('1964-07-20') should return 19640720, but when I run the SQL statement I get...
1
by: stefan.albert | last post by:
Hi folks, when subtracting 2 timestamps from each other the result is duration. () But in DB2 the format is a bit funny: lets say we have 59,5 seconds. If we have one second more, the result is:...
1
by: somadritadas23 | last post by:
I have a variable declaration inside a procedure which is not compiling in db2 9.5 :- DECLARE VSTARTTIME TIMESTAMP DEFAULT CURRENT TIMESTAMP; The procedure is a test procedure :- DROP...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.