Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying month name .. problem with script

Status
Not open for further replies.

davef101

MIS
Mar 3, 2006
34
GB
I need a script to display each months name .. but this only displays the varable name i.e $month10 ..


my $month1 = "January";
my $month2 = "February";
my $month3 = "March";
my $month4 = "April";
my $month5 = "May";
my $month6 = "June";
my $month7 = "July";
my $month8 = "August";
my $month9 = "September";
my $month10 = "October";
my $month11 = "November";
my $month12 = "December";


($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$mon = $mon+1;
$year = $year+1900;
$hour = sprintf("%02d", $hour);
$min = sprintf("%02d", $min);
$sec = sprintf("%02d", $sec);
$mday = sprintf("%02d", $mday);
$mon = sprintf("%02d", $mon);

$_ = '$month'.$mon;
$Current_Month = $_;

print "$Current_Month";

Result = $month10 not October ..

Any ideas?
 
use an array,
Code:
my @month;

$month[1] = "January";
$month[2] = "February";
$month[3] = "March";
$month[4] = "April";
$month[5] = "May";
$month[6] = "June";
$month[7] = "July";
$month[8] = "August";
$month[9] = "September";
$month[10] = "October";
$month[11] = "November";
$month[12] = "December";

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$mon = $mon+1;
$year = $year+1900;
$hour = sprintf("%02d", $hour);
$min =  sprintf("%02d", $min);
$sec =  sprintf("%02d", $sec);
$mday =  sprintf("%02d", $mday);
$mon =  sprintf("%02d", $mon);

$Current_Month = $month[$mon];

print "$Current_Month";

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I think you believe this:

Code:
$_ = '$month'.$mon;
$Current_Month = $_;

will translate to:

Code:
$month10 = 'October';

$_ will litearlly be the string $month10, it will not be a scalar $month10. When you single-quote a scalar '$month' the '$' symbol has no speacial meaning, it's just a dollar sign.

Use an array like 1DMF shows above, or better yet, use the POSIX module to format your date output.

- Kevin, perl coder unexceptional!
 
Code:
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime ( time );
my @months   = qw ( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my @weekdays = qw ( Sun Mon Tue Wed Thu Fri Sat );

print "$months[$mon]\n";
print "$weekdays[$wday]\n";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top