【发布时间】: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