如何将文件放入数组并将其保存在perl中 [英] how to put a file into an array and save it in perl

查看:85
本文介绍了如何将文件放入数组并将其保存在perl中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是 perl 的初学者,我正面临一些问题,因为我想将我的字符串从 AA 开始到 \ 放入一个数组中并想要保存它.txt 文件中大约有 2000-3000 个字符串,从相同的首字母开始,即 AA 到/我就是这样做的,如果我错了,请纠正我.

Hello everyone I'm a beginner in perl and I'm facing some problems as I want to put my strings starting from AA to \ in to an array and want to save it. There are about 2000-3000 strings in a txt file starting from same initials i.e., AA to / I'm doing it by this way plz correct me if I'm wrong.

AA  c0001
BB  afsfjgfjgjgjflffbg
CC  table
DD  hhhfsegsksgk
EB  jksgksjs
\
AA  e0002
BB  rejwkghewhgsejkhrj
CC  chair
DD  egrhjrhojohkhkhrkfs
VB  rkgjehkrkhkh;r
\

源代码

$flag = 0
while ($line = <ifh>)
{

    if ( $line = m//\/g)
    {
        $flag = 1;
    }
    while ( $flag != 0)
    {
        for ($i = 0; $i <= 10000; $i++)
        { # Missing brace added by editor
            $array[$i] = $line;
        } # Missing brace added by editor
    }
}  # Missing close brace added by editor; position guessed!
print $ofh, $line;

close $ofh;

推荐答案

这是一种将数据读入数组的方法.正如我在评论中所说,除非您更改它,否则将这些数据保存"到文件中是没有意义的.因为如果我将下面的 @data 数组打印到文件中,它看起来完全像输入文件.

Here's a way to read your data into an array. As I said in a comment, "saving" this data to a file is pointless, unless you change it. Because if I were to print the @data array below to a file, it would look exactly like the input file.

因此,您需要先告诉我们您想完成什么,然后我们才能给您答案.

So, you need to tell us what it is you want to accomplish before we can give you an answer about how to do it.

此脚本遵循以下规则(完全正确):

This script follows these rules (exactly):

  • 找到以AA"开头的一行,并将其保存到 $line
  • 连接每个新行文件到 $line
  • 当你发现一行以反斜杠 \,停止连接行并将 $line 保存到 @data 中.
  • 然后,找到下一行开始用AA"开始循环.
  • Find a line that begins with "AA", and save that into $line
  • Concatenate every new line from the file into $line
  • When you find a line that begins with a backslash \, stop concatenating lines and save $line into @data.
  • Then, find the next line that begins with "AA" and start the loop over.

这些匹配的正则表达式非常松散,因为它们也会匹配 AAARGH\bonkers.如果你需要它们更严格,你可以尝试 /^\\$//^AA$/,但是你需要注意开头和结尾的空格线.所以也许 /^\s*\\\s*$//^\s*AA\s*$/ 代替.

These matching regexes are pretty loose, as they will match AAARGH and \bonkers as well. If you need them stricter, you can try /^\\$/ and /^AA$/, but then you need to watch out for whitespace at the beginning and end of line. So perhaps /^\s*\\\s*$/ and /^\s*AA\s*$/ instead.

代码:

use warnings;
use strict;

my $line="";
my @data;

while (<DATA>) {
    if (/^AA/) {
        $line = $_;
        while (<DATA>) {
            $line .= $_;
            last if /^\\/;
        }
    }
    push @data, $line;
}

use Data::Dumper;
print Dumper \@data;

__DATA__
AA  c0001
BB  afsfjgfjgjgjflffbg
CC  table
DD  hhhfsegsksgk
EB  jksgksjs
\
AA  e0002
BB  rejwkghewhgsejkhrj
CC  chair
DD  egrhjrhojohkhkhrkfs
VB  rkgjehkrkhkh;r
\

这篇关于如何将文件放入数组并将其保存在perl中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆