Easier way to split a string

Posted on 2006-03-24
Last Modified: 2010-04-17
I have got a date in the format "yyyymmdd".
I would like a one liner in PERL that can split the above string into $y,$m and $d.

For example,
if $year is "19810203", then i want a PERL one liner that can split this into 3 parts storing into 3 variables.
That is the 3 variables $y,$m and $d should have "1981","10" and "03" respectively.

Question by:vinny_r78
    LVL 11

    Expert Comment

    ($y, $m, $d) = (substr($teststr,0,4),substr($teststr,4,2),substr($teststr,6,2));
    LVL 11

    Accepted Solution

    Or in your example

     ($y, $m, $d) = (substr($year,0,4),substr($year,4,2),substr($year,6,2));

    Author Comment

    Thanks DeanHorak.
    It is working.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    Title # Comments Views Activity
    bigDiff challenge 17 57
    sum13 challenge 24 68
    Batch file output 20 71
    WMI, model #, retrieving information 10 50
    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now