Perl 的重命名 - 如何附加计数器? [英] Perl's rename - how to append a counter?

查看:54
本文介绍了Perl 的重命名 - 如何附加计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个很好的重命名实用程序,它随 Perl 安装一起提供.如何在 Perl 正则表达式中附加一个计数器?这是一个相关的问题,例如对于当前目录下的文件编号问题:

There's a nice renaming utility, which comes with Perl's installation. How one would append a counter in the Perl regexp? This is a relevant question e.g. for a problem of numbering files in the current directory:

rename 's/^/<here I'd like to have a number of a file being processed>/g' * 

例如如何重命名:

fileA
fileB
fileC

1 - fileA
2 - fileB
3 - fileC

编辑:

我添加了计数器功能($c 变量) - 参见 这里.它工作正常 - 但是当我尝试指定计数器格式时:

I've added the counter feature ($c variable) - see here. It works fine - but when I try to specify the counter format:

rename_c.pl -c 5.2f 's/^/$c - /' * 

它说:

 Useless use of concatenation (.) or string in void context at line 120. 

它并没有真正使用我告诉它使用的格式.这一定是第 120 行中的一些简单的语法错误.您能看一下吗?

and it doesn't really use the format I told it to use. This must be some simple syntax mistake in a line number 120. Can You please take a look?

推荐答案

有问题的代码行是:

$c = sprintf(eval("%" . "$form", $cNumber));

你不希望 eval 出现在那里;您可以简单地将格式创建为字符串:

You don't want the eval there; you can simply create the format as a string:

$c = sprintf("%$form", $cNumber));

字符串(第一个参数)最终包含请求的格式,sprintf() 使用它格式化 $cNumber.

The string (first argument) ends up containing the format requested, and sprintf() formats $cNumber using that.

这篇关于Perl 的重命名 - 如何附加计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆