我的图像文件名由四部分组成:
-
$Directory
(图像所在的目录)
-
$Name
(对于艺术网站,这是画作名称参考#)
-
$File
(图像文件名减去扩展名)
-
$Extension
(图像扩展名)
$example 100020003000.png
我希望对其进行相应的细分:
$dir=1000 $name=2000 $file=3000 $ext=.png
我想知道 substr 是否是分解传入的最佳选择$example
所以我可以用 4 个变量做一些事情,比如验证/错误检查,从它的详细名称中获取$Name
作业或其他什么。我找到了这个帖子:
unpack 比 substr 更快吗? https://stackoverflow.com/questions/1083269/is-perls-unpack-ever-faster-than-substr所以,在我的初学者“石器”方法中:
my $example = "100020003000.png";
my $dir = substr($example, 0,4);
my $name = substr($example, 5,4);
my $file = substr($example, 9,4);
my $ext = substr($example, 14,3); # will add the the "." later #
那么,我可以使用 unpack,或者甚至是另一种更有效的方法吗?
我还想避免加载任何模块,除非这样做会因某种原因使用更少的资源。模组是我喜欢的很棒的工具,但我认为在这里没有必要。
我意识到我可能应该将变量推入数组/哈希中,但是,我真的是一个初学者,我需要进一步的指导来了解如何做到这一点以及如何将它们拉出来。
感谢 stackoverflow.com 上的每个人!