Question:
Perl array question...?
JoeSchmo5819
2007-09-24 11:18:28 UTC
Can anyone explain why I am going an "uninitialized value in concatenation" error when I try to run the code below? I'm trying to assign elements of the created array to variables to use them later. Thanks!

#!/usr/local/bin/perl -w

print "Enter date in YYYY-MM-DD-TTTT format: \n";
$data = ;
chomp $data;

@date = split(/-/, $data);

$date[0] = $year;
$date[1] = $month;
$date[2] = $day;
$date[3] = $time;

print "$year \n";
print "$month \n";
print "$day \n";
print "$time \n";
Three answers:
2007-09-24 11:36:58 UTC
You have your assignments backward:



$date[0] = $year;

$date[1] = $month;

...etc..



These should be:



$year = $date[0];

$month = $date[1];



..... to make this make sense, however, this is really kind of silly.



Try this on for size:



#!/usr/local/bin/perl -w



use strict;



my ($userInput,$year,$month,$day,$time);



do

{

print "Enter date in YYYY-MM-DD-TTTT format: \n";

}

while (($userInput = ) !~ /\d{4}-\d\d-\d\d-\d{4}/);



($year,$month,$day,$time) = split(/-/, $userInput);

print "Year: $year Month: $month Day: $day Time: $time\n";
2007-09-24 18:25:49 UTC
$date[0] = $year;

$date[1] = $month;

$date[2] = $day;

$date[3] = $time;



isn't it the opposite ?



$time = $date[3];

etc...
a_talis_man
2007-09-24 18:28:39 UTC
code is backwards you want...

$year = $date[0];

$month = $date[1] ;





etc....


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...