【问题标题】:A simple copying code一个简单的复制代码
【发布时间】:2014-07-10 06:20:32
【问题描述】:

我需要一个简单的珍珠重复代码,它从一个文件中读取,假设 txt 输入数据,然后将新数据保存在另一个 txt 文件中。代码需要这样做:

在第一个txt文档中我们有一些这种格式的成果

  1. 2个苹果
  2. 3颗樱桃的
  3. 4个西红柿

所以代码需要做以下事情。输入 1. apples 然后它必须识别第二个数字,即它必须像这样写这个词的次数

  1. 苹果 -> 1-1

           apples
    

    2 。苹果 -> 1-2

               apples
    

之后对列表中的每个项目重复相同的过程

对于代码我不知道如何制作它我知道如何输入文件以及如何制作结果文件但仅此而已

#!/usr/bin/perl\
open FH "<", "filename.txt" or die $!;

open FHWRITE, ">>results.txt" or die $!;

$fruit = 
 #dont know how for the $ to take the value of the first fruit 

$countnumb = split (' ',);

 # dont know how to take the second number for count number but i think its whit split 

$count =0; 

while ($count ne $countnumb)
{
    print "$fruit";
    $count++;
}

 # i dont know when this finishes how to make it go for the second fruit 

【问题讨论】:

  • 尝试自己编写一些东西,如果不起作用,请具体向我们展示您所做的事情,以便我们为您提供帮助。 您启动它,然后我们提供帮助。我们不是为您编写的。 向我们展示您尝试过的实际代码,然后描述发生的情况和不正确的地方,然后我们可以从那里帮助您。如果您先自己尝试一下,您可能会非常接近答案。
  • @user3523886 您可能想阅读以下内容:eev.ee/blog/2011/04/13/perl-worst-practices

标签: perl list file counter copying


【解决方案1】:

一些起点

while (my $line = <FH>){
  chomp($line);
  ### process the line
  #1. 2 apples 
  if ($line =~ s!^(\d+)[\.\s]+(\d+)\s+(.+?)$!!is){
    my ($line_no,$count,$text) = ($1,$2,$3);
    ### write out
    print FHWRITE $text for (1..$count); 
  } else {
     say "Cannot process $line\n";
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-18
    • 2018-04-06
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多