简体   繁体   English

在Perl中,如何解压缩几个变量?

[英]In Perl, how can I unpack to several variables?

I have a struct wich contains: 我有一个包含的结构:

struct mystruct{
  int                id[10];
  char               text[40];
  unsigned short int len;
};

And I'm trying to unpack it in a single line, something like this: 我正在尝试将其包装成一行,如下所示:

  my(@ids,$text,$length) = unpack("N10C40n",$buff) ;

But everything is going to the first array(@ids), i've tried templates as " N10 C40 n " and " (N10)(C40)(n) " So, either this can't be done or I'm not using the proper template string. 但是,一切都将转到第一个数组(@ids),我已经尝试将模板分别为“ N10 C40 n ”和“ (N10)(C40)(n) ”使用正确的模板字符串。

Note: I'm using big endian data. 注意:我使用的是大端数据。

Any hints? 有什么提示吗?

In list assignment the first array or hash will eat everything (how would it know where to stop?). 在列表分配中,第一个数组或哈希将吃掉所有内容(它将如何知道从何处停止?)。 Try this instead: 尝试以下方法:

my @unpacked        = unpack "N10Z40n", $buff;
my @ids             = @unpacked[0 .. 9];
my ($text, $length) = @unpacked[10, 11];

you could also say 你也可以说

my @ids;
(@ids[0 .. 9], my ($text, $length)) = unpack "N10Z40n", $buff;

如果@ids的顺序无关紧要:

my ($length, $text, @ids) = reverse unpack("N10C40n",$buff) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM