perl面试题解析

perl面试题解析,第1张

概述数据分析 检测系统某一目录下是否存在文件a.txt,有的话,则mv成新的文件,没有,则创建它,并向文件中输入以下数字: 1       0       12       1       13       0       1 4       1       1 5       0       1 6       1       1 7       0       1 8       1

数据分析
@H_301_8@

检测系统某一目录下是否存在文件a.txt,有的话,则mv成新的文件,没有,则创建它,并向文件中输入以下数字:@H_301_8@

1       0       12       1       13       0       1 4       1       1 5       0       1 6       1       1 7       0       1 8       1       1

 @H_301_8@

面试中,对这个题目产生了歧义,一时紧张,竟然不知从何下手。回来仔细一想,so easy!@H_301_8@

1)出题者的本意解法@H_301_8@

#!/bin/env perl  use strict; if (-e 'a.txt') {    system "mv a.txt a.txt.old";  }  else {      system "touch a.txt"; }      open my $file,'>>','a.txt' or dIE "$!\n";    for(my $i=1;$i<9;$i++){        if ($i % 2) {              print $file "$i\t0\t1\n";     }             else {                  print $file "$i\t1\t1\n";              } }

2)产生歧义的解法@H_301_8@

#!/bin/env perl  use strict;  if (-e 'a.txt') {       system "mv a.txt a.txt.old"; } else {    system "touch a.txt";  }    open my $file,'a.txt' or dIE "$!\n";    print $file <<'EOF';   1       0       1   2       1       1   3       0       1   4       1       1   5       0       1   6       1       1   7       0       1   8       1       1   EOF      close $file;
总结

以上是内存溢出为你收集整理的perl面试题解析全部内容,希望文章能够帮你解决perl面试题解析所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1294451.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存