如何在数组中找到输入的长度并在perl中匹配模式 [英] how to find the lengh of the input in an array and match pattern in perl

查看:58
本文介绍了如何在数组中找到输入的长度并在perl中匹配模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 perl 脚本的新手我有一个这样的数组我的@array (0x0B,0x04,0x02767,0x0000,0xffffaf)我的预期输出:如果索引的长度超过 4(我假设 0x0B=4)然后做拆分并加入这样的字符串0x2767-->0x27,0x670xffffaf->0xff,0xff,0xaf

i am new to to the perl scripting i have an array like this my @array (0x0B,0x04,0x02767,0x0000,0xffffaf) my expected output: if the length of the index is more than 4(i have assumed 0x0B=4) then do the split and join the string like this 0x2767-->0x27,0x67 0xffffaf->0xff,0xff,0xaf

在这里我试图找出输入字符串的长度和匹配如果输入字符串的长度大于 2 并且匹配模式那么它应该满足if条件

here i am trying to find out the length of the input string and matching pattern if the length of the input string is more than 2 and match the pattern then it should be satisfy the if condition

my $input = "0x0B";
# Name of the file the data is in
my $input_filename  = 'my_input_original.txt';

# 要将输出转储到的文件的名称我的 $output_filename = 'my_org_output.txt';

# Name of the file you want to dump the output to my $output_filename = 'my_org_output.txt';

#打开文件打开我的 $input_fh, "<", $input_filename or die $!;

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

# 打开输出文件打开我的 $output_fh, ">", $output_filename or die $!;至我的 $output_filename = 'my_org_output.txt'我更新的代码我想做什么

# Open the output file open my $output_fh, ">", $output_filename or die $!; to my $output_filename = 'my_org_output.txt' my updated code what i want to do

here is what i want to do
my input data:
total_sum   0x0B    Uint8,unsigned char     
num     0x0C    Uint8,unsigned char     
max     0x4A    Uint8,unsigned char     
padd    0x00     Uint8,unsigned char        
ideal   0x01    Uint16, unsigned short      
newtext{2}  { 0x00, 0x00 }  Uint8,unsigned char     
my_new  0x02     Uint16, unsigned short     
newtest2{2} { 0x00, 0x00 }  Uint8,unsigned char     
xyz         0x0A     Uint8,unsigned char        
newtest4{3} { 0x00, 0x00, 0x00 }    Uint8,unsigned char
gtotal 0xffffffaf   Sint8,signed char
info    0x2767  Uint16, unsigned short
my_output:

0x0B,0x0C,0x4A,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x02,
0x00,0x00,0x0A,0x00,0x00,0x00

1.if the line contain Uint16 then pad with 0x00 with the value

2.if the line contain the value with 0x2767 split and join 
 with(e.g.0x2767->0x27,0x67) or 
 0xffffaf (0xffffaf->0xff,0xff,0xaf)
my code :
#here is extracting only hex value
while ($_ =~ m/(0x(\d+)(?:[0-9]|[A-f])+)/gi)
{
 push @hex_array, $1; # here i get only hex value of for my input data
 #here if the array contain index value more than two then i need to
 #split the data and join also if the line contain Uint16 then i need to 
 #padd with 0x00 before the value (for e.g. 0x04-->0x00,0x04)
  foreach my $element (@hex_array) 
    {
   my @parts;
  if (4 < length $element) 
   {
    pos $element = 2;  # skip 0x
    @hex_array = map "0x$_", $element =~ /../g;
 } else 
 {
    @hex_array = $element;
 }
}
}
# Write the data to the file
print {$output_fh} join("U, ", @hex_array);
please help me

推荐答案

听起来你有一堆 32 位的十六进制值,你想把它们打包成指定的大端类型,而且你想要打包结构的十六进制表示.

Sounds like you have a bunch of 32-bit values as hex, and that you want to pack them into the specified big-endian type, and that you want a hex representation of the packed structure.

(请注意,这假设您对 gtotal 的输出是错误的.如果我理解正确,0xffffffaf 表示 -81,即0xAF 作为 Sint8.)

(Note that this assumes you are wrong about the output for gtotal. If I understand correctly, 0xffffffaf means -81, which is 0xAF as a Sint8.)

use strict;
use warnings;
use feature qw( say );

my %packers = (
   Uint8  => sub { pack 'C*',  @_ },
   Sint8  => sub { pack 'c*',  @_ },
   Uint16 => sub { pack 'S>*', @_ },
   Sint16 => sub { pack 's>*', @_ },
   Uint32 => sub { pack 'L>*', @_ },
   Sint32 => sub { pack 'l>*', @_ },
);

my $packed = '';

while (<DATA>) {
   my ($vals, $type);
   if (/^\S+ \s+ \{ \s* ( 0x[0-9a-fA-F]+ (?: \s* , \s* 0x[0-9a-fA-F]+ )* ) \s* \} \s+ ([^\s,]+) /x) {
      $vals = $1;
      $type = $2;
   }
   elsif (/^\S+ \s+ ( 0x[0-9a-fA-F]+ )\s+ ([^\s,]+) /x) {
      $vals = $1;
      $type = $2;
   }
   else {
      die("Unrecognized syntax at line $.\n");
   }

   my @vals =
       map { unpack 'l', pack 'l', hex $_ }  # Convert to number and fix the sign
          split /\s*,\s*/,
             $vals;

   my $packer = $packers{$type}
      or die("Unsupported type \"$type\"\n");

   $packed .= $packer->(@vals);
}

# say sprintf '%v02X', $packed;

say
   join ',',
      map { sprintf("0x%02X", $_) }
         unpack 'C*',
            $packed;

__DATA__
total_sum   0x0B    Uint8,unsigned char
num     0x0C    Uint8,unsigned char
max     0x4A    Uint8,unsigned char
padd    0x00     Uint8,unsigned char
ideal   0x01    Uint16, unsigned short
newtext{2}  { 0x00, 0x00 }  Uint8,unsigned char
my_new  0x02     Uint16, unsigned short
newtest2{2} { 0x00, 0x00 }  Uint8,unsigned char
xyz         0x0A     Uint8,unsigned char
newtest4{3} { 0x00, 0x00, 0x00 }    Uint8,unsigned char
gtotal 0xffffffaf   Sint8,signed char
info    0x2767  Uint16, unsigned short

输出:

0x0B,0x0C,0x4A,0x00,0x00,0x01,0x00,0x00,0x00,0x02,0x00,0x00,0x0A,0x00,0x00,0x00,0xAF,0x27,0x67

这篇关于如何在数组中找到输入的长度并在perl中匹配模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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