perl -- 在连接末尾添加双引号和直角括号

#!/usr/bin/perl -w

# prints dir list of .jpg filenames to screen

# and adds HTML markup for flexbox


use strict;

use warnings;

use Text::Autoformat;

my $file;

my $nfilename;

my $first = '<img src="';

my $last = '" style="width:100%"> ';

my $title = 'title = "';

my $estr = '">';

my $dir = "/home/clair/cp-perl/";

my $mylist;

opendir(DIR, $dir) or die $!;


while ($file = readdir(DIR)) {


# Use a regular expression to ignore files beginning with a period

next if ($file =~ m/^\./);

next if (substr $file, -1) ne "g";


#***************************************

# get rid of extension and replace hyphen with space   


$nfilename=$file;

$nfilename=~s/.jpg//;

$nfilename =~ s/-/ /g;


# ****************************************

#capitalizewords in filename to be a title


my $formatted = autoformat $nfilename, { case => 'highlight' };


chomp($nfilename);


# ****************************************

$mylist = join("",$first, $file, $last, $title, $nfilename, $estr);


# ************************* 

# thanks to George Mavridis - stackoverflow

$mylist =~ s/[\r\n]+//;

$mylist .="\n";

# *************************


print $mylist;


}

closedir(DIR);

exit 0;

这是我现在得到的前 3 行输出:


<pre>

<img src="out-of-the-night.jpg" style="width:100%"> title = "Out of the Night

"><img src="homage-to-borgeson.jpg" style="width:100%"> title = "Homage to Borgeson

"><img src="autumn-in-vermont.jpg" style="width:100%"> title = "Autumn in Vermont

</pre>

前两个字符应该位于行的末尾,如下所示:


<pre>

<img src="out-of-the-night-sm.jpg" style="width:100%"> title = "Out of the Night">

<img src="homage-to-borgeson.jpg" style="width:100%"> title = "Homage to Borgeson">

<img src="autumn-in-vermont-sm.jpg" style="width:100%"> title = "Autumn in Vermont">

</pre>

这是连接线:


$mylist = join("",$first, $file, $last, $title, $nfilename, $estr);

print $mylist;

这是 $estr 声明: my $estr = '">'; 我已经尝试了无数版本——这只是当前的一个。


我想知道如何使这两个字符显示在行的末尾而不是下一行的开头。


我在这件事上花了几个小时,前天还花了两个小时试图让论坛接受我的问题。



BIG阳
浏览 93回答 3
3回答

缥缈止盈

中的字符串$nfilename显然以换行符结尾。您是否从文件中读取了一行并保留了换行符?chomp可用于从变量中删除尾随换行符。

莫回无

有数百种方法可以解决这个问题,其中之一是:您的示例中的 $mylist (打印之前)包含:<img&nbsp;src="out-of-the-night.jpg"&nbsp;style="width:100%">&nbsp;title&nbsp;=&nbsp;"Out&nbsp;of&nbsp;the&nbsp;Night ">删除中间的换行符,并将其添加到字符串的末尾$mylist&nbsp;=~&nbsp;s/[\r\n]+//; $mylist&nbsp;.="\n";会解决这个问题。顺便说一句: $last 末尾的 '> "' 似乎也是错误的。

吃鸡游戏

请看看您是否发现以下代码有用。它在本地目录中查找 JPG 文件并生成网页use strict;use warnings;use feature 'say';my $dir = '.';my @files;push @files, $_ while glob('*.jpg');my $title = 'Pictures in JPEG format';my $space = "\n\t\t\t";my $style = 'width:100%';my $html&nbsp; = '<html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <title>$title</title>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>';for (@files) {&nbsp; &nbsp; /(.*?)\.jpg/;&nbsp; &nbsp; my $title = $1;&nbsp; &nbsp; $title =~ s/[-_]/ /g;&nbsp; &nbsp; $html .= "$space title = \"\u$title\"";&nbsp; &nbsp; $html .= "$space<img src=\"$_\" style=\"$style\">";}$html .= '&nbsp; &nbsp; </body></html>';say $html;输出<html>&nbsp; &nbsp; &nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <title>Pictures in JPEG format</title>&nbsp; &nbsp; &nbsp; &nbsp; </head>&nbsp; &nbsp; &nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;title = "File 08"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="file-08.jpg" style="width:100%">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;title = "File 09"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="file-09.jpg" style="width:100%">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;title = "File 01"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="file_01.jpg" style="width:100%">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;title = "File 02"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="file_02.jpg" style="width:100%">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;title = "File 03"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="file_03.jpg" style="width:100%">&nbsp; &nbsp; &nbsp; &nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5