免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 3112 | 回复: 20
上一主题 下一主题

还是跑来求帮助了... [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-05-02 07:22 |只看该作者 |倒序浏览
第一题吧:
Write a Perl program P1.pl to greet whatever is passed to it on the command line,
e.g.,
Hello, stranger!
There can be a problem with the above script: a parameter must be supplied, otherwise we'll get an error message.

comp315@turing> ./P1.pl
Use of uninitialized value $thing in concatenation (.)
or string at ./P1.pl line 4.
Hello, !
You should write the script in such a way that it will print a default value, let's say
\world", if no value is supplied on the command line.
comp315@turing> ./P1.pl
Hello, world!
Pay attention when providing zero as a parameter, as the greeting should be
comp315@turing> ./P1.pl 0
Hello, 0!
Modify the script to say \Goodbye" according to whether the -b switch was used on
the command line and the corresponding comment will be the output:
>P1.pl                             # Hello, world!
>P1.pl student                # Hello, student!
>P1.pl -b                        # Goodbye, world!
>P1.pl -b 'nice student'   # Goodbye, nice student!


第二题吧, 改错这个我没怎么看懂有谁能帮我看看咩
The following program is supposed to read text from standard input and to output
them sorted by length. The lines are read in an array called @lines. Based on
@lines, a new array @sortarray is created such that each element of this array is the
length of a line of text, concatenated with the vertical bar `|' character, concatenated
with the index of that line of text in the original array @lines. The program sorts
the new array, and then uses the results to display the original lines in sorted order.

#!/usr/bin/perl
use strict;
use warnings;

my @lines;
my @sortarray;
# read in the entire input
@lines = <STDIN>;

#construct the array with the length prepended
foreach (0 .. $#lines) {
  $sortarray[$_] = length($lines[$_]) . "|" . $_;
}

# sort using numeric comparison
@sortarray = sort { $a <=> $b } @sortarray;

# display results
print "****************************************\n";
foreach (@sortarray) {
  print $lines[substr($_, index($_, "|"))];
}



论坛徽章:
0
2 [报告]
发表于 2013-05-02 21:14 |只看该作者
請問第一題可不可以使用module

论坛徽章:
0
10 [报告]
发表于 2013-05-03 09:22 |只看该作者
#!/usr/bin/perl
use strict;
use warnings;

my $person ='world';
if( @ARGV)
{
$person = $ARGV[0];
}
print "Hello ".$person," ! \n";
这是我写的东西...
好像没有用到回复 2# afukada


   

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
11 [报告]
发表于 2013-05-03 12:23 |只看该作者
本帖最后由 rubyish 于 2013-05-03 09:02 编辑

第一題:
  1. #!/usr/bin/perl
  2. use 5.016;
  3. @ARGV = map { $_ // 'world' } @ARGV[ 0, 1 ];
  4. my @greet = ( "Hello, $ARGV[0]!", "Goodbye, $ARGV[1]!" );
  5. say $greet[ $ARGV[0] eq '-b' ];
复制代码

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
12 [报告]
发表于 2013-05-03 15:15 |只看该作者
第二题:
改错
  1. my @sortarray;
  2. my @lines = <>;
  3. for ( 0 .. $#lines ) {
  4.     my $l = length $lines[$_];
  5.     $sortarray[$_] = [ $l, $l . "|" . $_ ];
  6. }
  7. @sortarray = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @sortarray;

  8. print "****************************************\n";
  9. for (@sortarray) {
  10.     print $lines[ substr( $_, index( $_, "|" ) + 1 ) ];
  11. }
复制代码
改写:

  1. my @lines = <>;
  2. print "****************************************\n";
  3. print map { $_->[1] } sort { $a->[0] <=> $b->[0] } map [ length, $_ ], @lines;
复制代码

论坛徽章:
0
15 [报告]
发表于 2013-05-04 14:25 |只看该作者
话说 改错这个 没怎么看懂.....回复 12# rubyish


   

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
16 [报告]
发表于 2013-05-06 13:55 |只看该作者
回复 13# babyma1109


    一个简洁优雅的代码 ...非常简短。

  1. my @a = map { $_ // 'world' } @ARGV[ 0, 1 ];
  2. say $a[0] ne '-b' ? "Hello, $a[0]!" :  "Goodbye, $a[1]!"
复制代码

论坛徽章:
1
15-16赛季CBA联赛之北控
日期:2016-08-05 14:22:52
17 [报告]
发表于 2013-05-10 14:53 |只看该作者
回复 16# rubyish


   请教一下  $_ // 'world' 是什么意思啊

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
18 [报告]
发表于 2013-05-11 09:00 |只看该作者
回复 17# 唐归来
  1. $_ // 'world' 是什么意思啊
复制代码
  1. defined $_ ? $_ : 'world'
复制代码

论坛徽章:
0
21 [报告]
发表于 2013-05-13 08:51 |只看该作者
不愧是大神级别的  比我这种屌丝写的好多了回复 20# ypqfyf


   

论坛徽章:
0
3 [报告]
发表于 2013-05-02 23:52 |只看该作者
回答了题是不是可以推荐工作?
sinian126 该用户已被删除
4 [报告]
发表于 2013-05-03 08:06 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
5 [报告]
发表于 2013-05-03 08:41 |只看该作者
NaN!!{:3_188:}{:3_188:}

论坛徽章:
0
6
发表于 2013-05-03 08:50
第一题 我只写出了第一部分....
怎么加-b让他交换就不太明白了
回复 2# afukada


   

论坛徽章:
0
7 [报告]
发表于 2013-05-03 08:53 |只看该作者
哈哈  想太多了  回答了就能写作业了
回复 3# Perlvim


   

论坛徽章:
0
8 [报告]
发表于 2013-05-03 08:54 |只看该作者
这个  话说我也是  可是米有办法
回复 4# sinian126


   

论坛徽章:
0
9
发表于 2013-05-03 08:55
老师还说...你要想拿extra point, 就要把甚么格式写的好看点啊
如果人家输入的是-c 你要告诉别人只能是-b才行甚么的  
很是头大啊回复 5# rubyish


   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP