Cheers :)#!/usr/bin/perl use strict; use warnings; my $s="20050725"; ############## # with substr my ($year, $month, $day) = ( substr($s, 0, 4), substr($s, 4, 2), subst +r($s, 6, 2)); print "gives : $year, $month, $day \n"; ############## # with a regexp ($year, $month, $day) = $s =~ /(\d{4})(\d\d)(\d\d)/ ; print "gives : $year, $month, $day \n" ; ############## # with split and join my @el=split '', $s; ($year, $month, $day) = ( join('', @el[0,1,2,3]), join('', @el[4,5]) +, join('', @el[6,7]) ) ; print "gives : $year, $month, $day \n" ; ############## # with shift @el=split '', $s; # zero the vars $year=$month=$day=undef; $year .= shift @el for 0..3; $month .= shift @el for 0..1; $day .= shift @el for 0..1; print "gives : $year, $month, $day \n" ;
In reply to Re: Splitting up a string by lengths
by wazoox
in thread Splitting up a string by lengths
by cosmicperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |