【问题标题】:separating array elements every 4 characters in array在数组中每 4 个字符分隔数组元素
【发布时间】:2016-10-10 09:18:45
【问题描述】:

我有一个这样的数组

my @array=(0x0B0x0C0x4A0x000x010x000x000x020)

我想为这 4 个字符中的每一个插入逗号,我的意思是

my @array=(0x0B,0x0C,0x4A,0x00,0x01,0x00,0x00,0x02)

我的 Perl 代码

#! /usr/bin/env perl
use strict;
use warnings;
my @hex_array;
# Name of the input file
my $input_filename = 'input.txt';

# Open the file
open my $input_fh, "<", $input_filename or die $!;

# Name of the output file
my $outut_filename = 'output.txt';

# Open the file
open my $output_fh, "<", $output_filename or die $!;

# reading input file by line by line.
while (<input_fh>)
{
    # here i am extracting all hex value
    while ($_ =~ m/(0x(\d+)(?:[0-9]|[A-f])+)/gi)
    {
        push @hex_array, $1; #push the element
    } # end of second while loop
} # end of first while loop

print @hex_array;

第一种方法

unpack("(A2)*", $hex_array);
print {$output_fh} join("U, ", @hex_array);

第二种方法

foreach my $element (@hex_array)
{
   if (length $element eq 4)
   {
       #print @hex_array;
       print {$output_fh} join("U, ", @hex_array);
   }
}

但是这两种方法都不起作用。什么是合适的解决方案?

【问题讨论】:

  • 这个很不清楚。那些my@array 行是您文件的文字内容吗?您是否尝试以编程方式更改一段源代码?两种方法是否在同一个程序中,或者您是否分别尝试过每种方法?
  • 我应该是我的@array,我在同一个程序上尝试了这两种方法,并且还分别尝试了它们中的每一个。
  • 在 -e 第 1 行,“0C0x4A0x000x010x000x000x020”附近的操作员预期的位置找到了裸字
  • @choroba 我认为第一个代码块是DATA 的内容。但最后有一个0 太多了。
  • 考虑到您的类似问题的历史没有被接受的答案,似乎很难帮助您

标签: perl


【解决方案1】:

如果该数组分配是它自己的行而不是多行,这将完全适用于您的示例。

use strict;
use warnings;

while (my $line = <DATA>) {
    # find and grab the chunk of hex values inside of the array assignment
    ( my $match ) = $line =~ m/\(((?:0x[0-9A-F]{2})+)\)/;

    next unless $match;

    # make a copy that we will change
    my $replaced = $match;

    # add a comma every four until the last one
    # and insert comma
    $replaced =~ s/(....)(?!$)/$1,/g;

    # but the replacement back into the current line
    $line =~ s/\Q$match\E/$replaced/;

    print $line;
}

__DATA__
my@array=(0x0B0x0C0x4A0x000x010x000x000x02)

一旦$line 的初始模式不匹配,它就会分解。另请注意,您的一长串十六进制数字的末尾还有一个额外的0。我的代码只有在你修复它时才有效。

输出:

my@array=(0x0B,0x0C,0x4A,0x00,0x01,0x00,0x00,0x02)

【讨论】:

  • 抱歉,我没有正确解释我的要求。输入我的@array 实际上是我从代码中收集的 hex_array 转储,现在我想用逗号分隔 4 个字符中的每一个的 hex_array 元素。
  • @abhi 我不知道你在说什么。请返回,编辑您的问题并使用正确缩进的代码清楚地解释。我们都在这里猜测。
【解决方案2】:

你没有

my @hex_array = ( 0x0B0x0C0x4A0x000x010x000x000x020 );

对于初学者来说,这甚至无法编译。您实际拥有的是以下内容:

my @hex_array = ( '0x0B', '0x0C', '0x4A', '0x00', '0x01', '0x00', '0x00', '0x02' );

得到

my @num_array = ( 0x0B, 0x0C, 0x4A, 0x00, 0x01, 0x00, 0x00, 0x02 );

简单地改变

push @hex_array, $1;

push @num_array, hex($1);

【讨论】:

    【解决方案3】:

    请试试这个:

    my @array = ("0x0B0x0C0x4A0x000x010x000x000x020");
    my @outarray = map { (my $vars = $_) =~ s/\w{4}/$&\,/g; $vars; } @array ;
    use Data::Dumper;
    print Dumper @outarray;
    

    Thanks to @derobert

    【讨论】:

    • I have tried your approach with adding more array elements I got output looks like :$VAR1 = '0x0B0x0C0x4A0x000x010x000x000x020x000x000x0A0x000x000x000x040x000x000x040x050x000x001f0x001f0x007f0x000x000x002b0x007f';
    • print Dumper @outarray;
    • 还有在数组中添加的更多元素中发现的垃圾字符。
    • @ssr1012,如果你检查代码,你会发现他们实际上有my @hex_array = ( '0x0B', '0x0C', '0x4A', '0x00', '0x01', '0x00', '0x00', '0x02' );
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多