in reply to Splitting up a string by lengths

I'm all in favor of being reasonably long-winded if it makes it clear what's going on to future maintenance programmers (or me two months later).
my $datestring = "20050725"; my $year = substr($datestring,0,4); my $month = substr($datestring,4,2); my $day = substr($datestring,6,2); print "$month/$day/$year\n";

Replies are listed 'Best First'.
Re^2: Splitting up a string by lengths
by TomDLux (Vicar) on Jul 25, 2005 at 17:58 UTC

    While other forms can be far worse, AND eror-prone, I'm not sure this qualifies as a shining example of readable and maintainable.

    I haven't used unpack very much outside dismantling dates, but I found it worth the inconvenience of learning the basic use of the function, because it is so so simple and clear, other than the horrific confusion of what all the codes signify, and which one means what. Luckily, named variables can conceal the format string.

    my ( $year, $month, $day ) = unpack $YYYYMMDD_format, $date_string;
    TomDLux

    --
    TTTATCGGTCGTTATATAGATGTTTGCA