Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

Unable to extract expected value through map function in perl

Not getting expected output for below input lines.

Basically what I am doing is I am extracting hex values and values inside curly braces.

  1. The values inside curly braces should be appended with 0x0 (eg 0x01)

  2. The value with uint16 should be splited for e.g(0x27,0x89)

  3. The negative value -116 I want to take 8C since its type is sint8 (for e.g -116--Hex is FFFFFF8C-->8C)

I am using map function for doing this, but I am getting output as some unexpected value.

My code:

use strict;
use warnings;

my @data;
while (<DATA>) 
{               
    my (@matches) = /(?|(0x[dA-F]+)|(?:{s*|s)(-?d+)(?!x))/g;
    #print "@matches
";
    push @data, ($1);
}

{
    my $data = join ',', map "0x0$_", map { unpack '(A2)*' } @data;     
    print $data, "
";    
}

__DATA__     
meas_command    PHY_MEAS_CONFIG { 0}    meas_command        
UE_position_meas_flag   else { 0}   UE_position_meas_flag       
reporting_interval  MCFE_L1C_RPTPERIOD { 1} reporting_interval      
fieldIndicator  tdd neighbor cell measurement indicator { 2}    fieldIndicator      
Reserved1   0x00    Uint8,unsigned char     
my_info    0x2789  Uint16, unsigned short        
filler1{3} { 0x00, 0x00, 0x00 }    Uint8,unsigned char     
rscp_tap   -116    Sint8,signed char              
rch_type    PCH { 7}    Uint8,unsigned char 
last_report 0   zeroth bit      
no_sync 0   oneth bit

Expected output:

 0x00,0x00,0x01,0x02,0x00,0x27,0x89,0x00,0x00,0x00,0x8c,0x07,0x00,0x00

What I was getting is:

0x00,0x00,0x01,0x02,0x00x,0x000,0x00x,0x027,0x089,0x00x,0x000,0x0-1,0x016,0x07,0x00,0x00

Something wrong is going with map function. Please help me ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not your map command, it's your regex.

Your captured data in this code is:

$VAR1 = [
          '0',
          '0',
          '1',
          '2',
          '0x00',
          '0x2789',
          '0x00',
          '-116',
          '7',
          '0',
          '0'
        ];

If you then:

my @newdata =  map { [unpack '(A2)*'] } @data;
print Dumper @newdata;

Then what you get is:

$VAR1 = [
          [
            '0'
          ],
          [
            '0'
          ],
          [
            '1'
          ],
          [
            '2'
          ],
          [
            '0x',
            '00'
          ],
          [
            '0x',
            '27',
            '89'
          ],
          [
            '0x',
            '00'
          ],
          [
            '-1',
            '16'
          ],
          [
            '7'
          ],
          [
            '0'
          ],
          [
            '0'
          ]
        ];

Which hopefully gives an insight into what's wrong? The unpack operation is returning a list when you do it, and even if it didn't - you're capturing some values with leading 0x and others without.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...