Gossamer Forum
Home : General : Perl Programming :

Split Field into Two Variables?

Quote Reply
Split Field into Two Variables?
I am opening a database file and reading the fields - One of the fields in the file is "Birthdate"

Can someone tell me how I can split this single field called "Birthdate" (i.e. 0203) into two variables like this "$month=02" and "$monthday=03"?

Or can this be done?

TIA!
------------
donm

Quote Reply
Re: Split Field into Two Variables? In reply to
Are all values in the BirthDate in the same format? If not, it will be virtually impossible to set conditions for different formats.

Regards,

------------------
Eliot Lee
Anthro TECH,L.L.C
www.anthrotech.com
* Be sure to visit the Resource Center for FAQ's, Modifications and Extra Goodies!!
* Search Forums!
* Say NO to Duplicate Threads. :)
----------------------








Quote Reply
Re: Split Field into Two Variables? In reply to
Yes - they are all in the same format as far as length "0101" and they are always 4 numbers.

-----------
donm
Quote Reply
Re: Split Field into Two Variables? In reply to
---------
#!/usr/local/bin/perl -w
use strict;
print "Content-type: text/plain\n\n";
my ($month, $day);
while(<DATA> ){
/(\d\d)(\d\d)/;
$month = $1;
$day = $2;
print "month:$month, day:$day\n";
}

__END__
0203
1215
0920
0625
-----------
Returns:

month:02, day:03
month:12, day:15
month:09, day:20
month:06, day:25

or

#!/usr/local/bin/perl -w
use strict;
print "Content-type: text/plain\n\n";
my ($month, $day);
my $date = '0203';

$date=~/(\d\d)(\d\d)/;
$month = $1;
$day = $2;

print "date:$date month:$month day:$day";

Returns:

date:0203 month:02 day:03