Perl 提示用户输入,然后在相应字段中进行加减
我希望从Perl 提示用户输入,然后在相应字段中进行加减,perl,bash,Perl,Bash,我希望从$a[3]中减去变量$cpySold,并将其添加到$a[4]。我怎么做 目前我的输出如下: Title:Alice in wonderland Author:robert No Of Copies Sold:*3* Current Book Info: Alice in wonderland, robert,$12.40,100,200 我怎么做下面这行?假设100-3=97,用户输入3份售出后,100+3=103 新书信息:罗伯特·爱丽丝梦游仙境,12.40,97203美元 我不喜
$a[3]
中减去变量$cpySold
,并将其添加到$a[4]
。我怎么做
目前我的输出如下:
Title:Alice in wonderland
Author:robert
No Of Copies Sold:*3*
Current Book Info:
Alice in wonderland, robert,$12.40,100,200
我怎么做下面这行?假设100-3=97,用户输入3份售出后,100+3=103
新书信息:罗伯特·爱丽丝梦游仙境,12.40,97203美元
我不喜欢在代码中混合使用shell和Perl,但这显然是出于教学原因,所以我们不得不忽略它
process_book_sold()
{
read -p "Title: " title
read -p "Author: " author
read -p "No Of Copies Sold : " cpySold
if [ -n "$title" -a -n "$author" ]; then
perl -ne '
BEGIN{ $title = shift; $author = shift; $sales = shift; }
@a = split /:/;
if ($a[0] =~ m/$title/i and $a[1] =~ m/$author/i)
{
print "Current Book Info:\n";
print "$a[0], $a[1], $a[2], $a[3], $a[4]\n";
$a[3] -= $sales;
$a[4] += $sales;
print "New Book Info:\n";
print "$a[0], $a[1], $a[2], $a[3], $a[4]\n";
}
END{ print "\n" }' "$title" "$author" "$cpySold" /home/student/Downloads/BookDB.txt
fi
}
除了将pattern
重命名为title
和pattern1
重命名为author
,此代码还将shell变量$cpySold
传递给Perl。它还使用一种更简单的方法来检索前三个参数(只需从shift
中捕获值)。拆分
与以前相同。数据文件中的格式并不完全清楚,因为打印格式使用逗号而不是冒号来分隔字段
我只想用newbook info中的值替换
BookDB.txt
文件中的当前图书信息
我不相信这对你有任何好处(除非你自己尝试,否则你不会学到很多),但是
显然,这不是我第一次运行脚本,有时我会使用3以外的值来计算售出的拷贝数
通过显式文件管理,您可以编写:
process_book_sold()
{
title="$1"
author="$2"
cpySold="$3"
if [ -n "$title" -a -n "$author" ]; then
perl -we '
use strict;
use English "-no_match_vars";
my $title = shift;
my $author = shift;
my $sales = shift;
my $file = shift;
open my $fh, "+<", $file or die "Failed to open file $file for reading and writing";
my $text;
{
local $/;
$text = <$fh>;
}
chomp $text;
my @a = split /:/, $text;
print "Debug: @a\n";
if ($a[0] =~ m/$title/i and $a[1] =~ m/$author/i)
{
print "Current Book Info:\n";
print "$a[0], $a[1], $a[2], $a[3], $a[4]\n";
$a[3] -= $sales;
$a[4] += $sales;
print "New Book Info:\n";
print "$a[0], $a[1], $a[2], $a[3], $a[4]\n";
seek $fh, 0, 0;
truncate $fh, 0;
$OFS = ":";
$ORS = "\n";
print $fh @a;
}
close $fh;
' "$title" "$author" "$cpySold" BookDB.txt # /home/student/Downloads/BookDB.txt
fi
}
# read -p "Title: " title
# read -p "Author: " author
# read -p "No Of Copies Sold : " cpySold
process_book_sold "Alice in Wonderland" "Carroll" "7"
为什么要使用bash和perl?为什么不直接使用perl呢?这是赋值要求,如果数据包含100和200,那么是100的减法和200的加法,还是相反?(…Hmmm…好的;所需的输出指示从100减去,然后添加到200…)您的代码当前有一行
打印“$a[0],$a[1],\$$a[2],$a[3],$a[4]\n”
这让人费解:为什么对$a[2]
进行不同的处理?您将从perl命令行初始化的变量的范围限制在BEGIN块,并且没有启用警告或strict来通知您所产生的问题。更准确地说,我根本没有运行代码……但您是对的。@JonathanLeffler,很抱歉再次打扰您,我想获取新书信息并替换bookDB.txt中的当前图书信息,可以吗?您还没有真正显示文件的格式。特别是,它是包含一本书的一条记录,还是包含多本书的多条记录?是的,这是可能的。问题中没有足够的信息来说明什么是有意义的。(另外,格式是这样的,带有子标题的书籍不会整齐地存储-字段的冒号分隔符会被标题和子标题之间的冒号所混淆。然而,这可能也是本练习的给定内容。)bookDB.txt包含标题:author:price:qtyavailable:qtySold
process_book_sold()
{
title="$1"
author="$2"
cpySold="$3"
if [ -n "$title" -a -n "$author" ]
then
perl -i -we '
use strict;
use English "-no_match_vars";
my $title = shift;
my $author = shift;
my $sales = shift;
while (<>)
{
chomp;
my @a = split /:/;
print STDERR "Debug: @a\n";
if ($a[0] =~ m/$title/i and $a[1] =~ m/$author/i)
{
print STDERR "Current Book Info:\n";
print STDERR "$a[0], $a[1], $a[2], $a[3], $a[4]\n";
$a[3] -= $sales;
$a[4] += $sales;
print STDERR "New Book Info:\n";
print STDERR "$a[0], $a[1], $a[2], $a[3], $a[4]\n";
$OFS = ":";
$ORS = "\n";
print @a;
}
}
' "$title" "$author" "$cpySold" BookDB.txt # /home/student/Downloads/BookDB.txt
fi
}
# read -p "Title: " title
# read -p "Author: " author
# read -p "No Of Copies Sold : " cpySold
process_book_sold "Alice in Wonderland" "Carroll" "3"
$ cat BookDB.txt; bash pbs2.sh; cat BookDB.txt
Alice in Wonderland:Carroll:$12.40:74:226
Debug: Alice in Wonderland Carroll $12.40 74 226
Current Book Info:
Alice in Wonderland, Carroll, $12.40, 74, 226
New Book Info:
Alice in Wonderland, Carroll, $12.40, 71, 229
Alice in Wonderland:Carroll:$12.40:71:229
$
process_book_sold()
{
title="$1"
author="$2"
cpySold="$3"
if [ -n "$title" -a -n "$author" ]; then
perl -we '
use strict;
use English "-no_match_vars";
my $title = shift;
my $author = shift;
my $sales = shift;
my $file = shift;
open my $fh, "+<", $file or die "Failed to open file $file for reading and writing";
my $text;
{
local $/;
$text = <$fh>;
}
chomp $text;
my @a = split /:/, $text;
print "Debug: @a\n";
if ($a[0] =~ m/$title/i and $a[1] =~ m/$author/i)
{
print "Current Book Info:\n";
print "$a[0], $a[1], $a[2], $a[3], $a[4]\n";
$a[3] -= $sales;
$a[4] += $sales;
print "New Book Info:\n";
print "$a[0], $a[1], $a[2], $a[3], $a[4]\n";
seek $fh, 0, 0;
truncate $fh, 0;
$OFS = ":";
$ORS = "\n";
print $fh @a;
}
close $fh;
' "$title" "$author" "$cpySold" BookDB.txt # /home/student/Downloads/BookDB.txt
fi
}
# read -p "Title: " title
# read -p "Author: " author
# read -p "No Of Copies Sold : " cpySold
process_book_sold "Alice in Wonderland" "Carroll" "7"
$ cat BookDB.txt; bash pbs1.sh; cat BookDB.txt
Alice in Wonderland:Carroll:$12.40:50:250
Debug: Alice in Wonderland Carroll $12.40 50 250
Current Book Info:
Alice in Wonderland, Carroll, $12.40, 50, 250
New Book Info:
Alice in Wonderland, Carroll, $12.40, 43, 257
Alice in Wonderland:Carroll:$12.40:43:257
$