免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2452 | 回复: 7
打印 上一主题 下一主题

How to input an array from keyboard? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-05 03:23 |只看该作者 |倒序浏览
I can easily input a string to a variable from keyboad using $v=<STDIN>, but how can I input an array or list to an @v? I use dos command and perl Express 2.5. Do I need to use some specific symbols to seperate strings when trying to input from keyboard?

Here is the code:

#!/usr/bin/perl

#Question: How to input array under DOS or in Perl Express for window?
@userinput1= ('name','6');
print @userinput1;  #return name6

# but nomatter under DOS or in Perl Express Std.Output I/O box;
@userinput2=<STDIN>; #input name 6 or name, 6 or 'name','6' from Keyboard
print @userinput2; # Nothing return

论坛徽章:
1
未羊
日期:2014-09-08 22:47:27
2 [报告]
发表于 2009-02-05 08:07 |只看该作者
原帖由 moreyale 于 2009-2-5 03:23 发表
I can easily input a string to a variable from keyboad using $v=, but how can I input an array or list to an @v? I use dos command and perl Express 2.5. Do I need to use some specific symbols to  ...
  1. chomp($str = <>);
  2. @array = split /\s+/,$str;
复制代码

各数组元素之间用空格分开,呵呵。

[ 本帖最后由 wxlfh 于 2009-2-5 08:13 编辑 ]

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
3 [报告]
发表于 2009-02-05 09:40 |只看该作者
# but nomatter under DOS or in Perl Express Std.Output I/O box;
@userinput2=<STDIN>; #input name 6 or name, 6 or 'name','6' from Keyboard
print @userinput2; # Nothing return

dos下没输出吗

论坛徽章:
0
4 [报告]
发表于 2009-02-05 21:58 |只看该作者

Thanks, but

Thank you wxlfh, I knew the method you recommand. I am just wandering how to process the usual code which textbooks taught us---"@v=<STDIN>;" It seems there is a way to input from keyboard directly. Or there should not be a code like this, it was a bug in the kernal of Perl, but not fatal bug.

论坛徽章:
0
5 [报告]
发表于 2009-02-05 22:51 |只看该作者
敲完之后别忘了敲个回车
然后用ctrl+D结束输入

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
6 [报告]
发表于 2009-02-05 23:26 |只看该作者
原帖由 moreyale 于 2009-2-5 21:58 发表
Thank you wxlfh, I knew the method you recommand. I am just wandering how to process the usual code which textbooks taught us---"@v=;" It seems there is a way to input from keyboard directly. Or  ...

STDIN 是可以被重定向到文件,或者是其他程序的输出,并不一定是键盘。

若是键盘,你可以用 Ctrl-D (在 Unix-like 系统下) 标示输入已经结束。

论坛徽章:
3
戌狗
日期:2014-09-10 17:07:162015年辞旧岁徽章
日期:2015-03-03 16:54:15wusuopu
日期:2016-06-17 17:43:45
7 [报告]
发表于 2009-02-06 00:00 |只看该作者
原帖由 ynchnluiti 于 2009-2-5 09:40 发表

dos下没输出吗

看错了。还以为是dos下没输出呢

论坛徽章:
1
未羊
日期:2014-09-08 22:47:27
8 [报告]
发表于 2009-02-06 07:55 |只看该作者
  1. chomp(@array = <>);
复制代码

那可以这样,每行就是一个数组元素,结束在Windows下用Ctrl+Z。
用chomp函数是去掉行尾的换行符。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP