• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[OReilly_Learning_Perl_5th_Edition]_Chap05_Exercises

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

2. [8] Write a program that asks the user to enter a list of strings on separate lines,printing each string in a right-justified, 20-character column. To be certain that the output is in the proper columns, print a “ruler line” of digits as well. (This is simply a debugging aid.) Make sure that you’re not using a 19-character column by mistake! For example, entering hello, good-bye should give output something like this:
123456789012345678901234567890123456789012345678901234567890
                            hello
                     good-bye
3. [8] Modify the previous program to let the user choose the column width so that entering 30, hello, good-bye (on separate lines) would put the strings at the 30th column. (Hint: see the section in Chapter 2 about controlling variable interpolation.) For extra credit, make the ruler line longer when the selected width is larger.

 

solution
#!perl -w
use strict;
use 5.010;

print "Please input string's width: ";
chomp(my $width = <>);

say
"1234567890" x ($width / 10 + 1);

printf "%".$width."s\n", "Hello World!";
printf "%${width}s\n", "Hello World!";

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
shell perl 等发布时间:2022-07-22
下一篇:
Perl教学子程序发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap