我需要用 perl 和 mysql 生成这样的案例编号:
xx-201012080001
第一部分只是几个字符,然后是日期。这 最后四位数字是问题所在,它应该从 0001 开始并递增 每个案例一个。真正的麻烦是它必须在 0001 重新启动 每一天。
关于如何做到这一点有什么建议吗?
最佳答案
使用 sprintf 生成字符串:
my $casenumber = sprintf("%2s-%8d%04d", $twochars, $date, $increment);
例如,在命令行上:
$ perl -e 'printf("%2s-%8d%04d\n", "aa", "20101211", 2);'
$ aa-201012110002
至于将计数重置为 1,请为迭代中的每条记录保留一个名为 $lastDate 的变量。如果 $lastDate != $currentDate,您将计数器重置为 1。
例如:
# assumes @records contain arrayrefs with relevant info from mysql
# also assumes the data is sorted by date.
my $count = 1;
my $lastDate;
my $currentDate;
for my $record (@records)
{
my $twochars = $record->[0];
my $currentDate = $record->[1];
if($currentDate != $lastDate)
{
$count = 1;
}
my $casenumber = sprintf("%2s-%8d%04d", $twochars, $currentDate, $count++);
$lastDate = $currentDate;
}
关于mysql - 生成案件编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4392282/