電子書籍の厳選無料作品が豊富!

スクリプト構文を大きく変更せずにabcdefghiと表示させるにはどこを修正すればよいでしょうか?

[server ~/]$ uname -a
FreeBSD xxxxxxx.xxxxxx.xx.jp 7.1-RELEASE-p4 FreeBSD 7.1-RELEASE-p4 #1: Mon Mar 23 17:35:54 JST 2009 admin@www200.sakura.ne.jp:/usr/obj/usr/src/sys/SAKURA11S i386
[server ~/]$ perl -v
This is perl, v5.8.9 built for i386-freebsd-64int
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2008, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
[server ~/]$ cat test.pl
#! /usr/bin/perl
$a = "abc@def@ghi";
while ($a =~ /\@/) { $a =~ s/\@//; }
print "$a\n";
[server ~/]$ ./test.pl
abc
[server ~/]$

A 回答 (3件)

文字列としてスクリプト中に書いてなければ大丈夫.


あと, while で「@がなくなるまで置換する」ってのはわかる. わかるんだけど, それだけなら while を使わずとも s/@//g なり tr/@//d なりすればいいんじゃないのかってのが #2 の「でも」以下の趣旨.
    • good
    • 0
この回答へのお礼

ありがとうございます。
大変参考になりました。
whileは使わない方向で修正リリースしたいと思います。

お礼日時:2009/04/03 04:10

もしくは @ をエスケープする.


でもこの while って何の意味があるんだろう.
$a =~ s/\@//g;
$a =~ tr/@//d;
のどちらかでいいのでは?

この回答への補足

whileは繰り返し置換です。
文字列に@が出現するたびに置換します。
@がなくなるまで置換するということです。

補足日時:2009/04/02 23:49
    • good
    • 0

> $a = "abc@def@ghi";



@def, @ghi が変数と見られないよう二重引用符ではなく、
一重引用符でくくればよいです。
    • good
    • 0
この回答へのお礼

ありがとうございます。成功です。
追加でご教示ください。
ファイルのアップロードフォームから取得したファイル名を
以下のように置換したいのですが、
この場合でもシングルクォートを使った場合と同等とみなしてよいでしょうか?
それとも途中で@を特別に処理する必要があるでしょうか?

my $fh = $cgi->upload('inputfilename') or &fail();
my $filename = File::Basename::basename($fh);
utf8::decode($filename);
while ($filename =~ /\@/) { $filename =~ s/\@//; }

お礼日時:2009/04/03 00:00

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!