Chinaunix

标题: 求助:如何把一个常量改成变量 [打印本页]

作者: fubzio    时间: 2014-03-14 17:03
标题: 求助:如何把一个常量改成变量
不好意思,超级菜鸟,问一个弱智问题:

use constant reply_tab => {
    0x80000000 => { cmd => 'generic_nack', reply => undef, },
    0x00000001 => { cmd => 'bind_receiver',
                    reply => sub { my ($me,$pdu) = @_;
                                   $me->set_version(0x34);
                                   $me->bind_receiver_resp(system_id => $sysid,
                                                           seq => $pdu->{seq});
                               }, },
    0x80000001 => { cmd => 'bind_receiver_resp', reply => undef, },
    0x00000002 => { cmd => 'bind_transmitter',
                    reply => sub { my ($me, $pdu) = @_;
                                   $me->set_version(0x34);
                                   warn "Doing bind_tx_resp";
                                   $me->bind_transmitter_resp(system_id => $sysid,
                                                           seq => $pdu->{seq});
                               }, },
    0x80000002 => { cmd => 'bind_transmitter_resp', reply => undef, },
    0x00000003 => { cmd => 'query_sm',
                    reply => sub { my ($me, $pdu) = @_;
                                   $me->query_sm_resp(message_id=>$pdu->{message_id},
                                                      final_date=>'010711135959000+',
                                                           seq => $pdu->{seq},
                                                      ) }, },
    0x80000003 => { cmd => 'query_sm_resp', reply => undef, },     
    0x00000004 => { cmd => 'submit_sm',
                    reply => sub { my ($me, $pdu) = @_;
                                   $me->submit_sm_resp(message_id=>'123456789',
                                                       seq => $pdu->{seq}) }, },
    0x80000004 => { cmd => 'submit_sm_resp', reply => undef, },
    0x00000005 => { cmd => 'deliver_sm',
        reply => sub { my ($me, $pdu) = @_;
          $me->deliver_sm_resp(message_id=>'123456789',
                  seq => $pdu->{seq}) }, },
    0x80000005 => { cmd => 'deliver_sm_resp', reply => undef, },


脚本写死了message_id=>'123456789',想改成: message_id等于一个随机产生基本不会重复的值(若能按顺序增长更好),请热心朋友看看如何修改?



作者: jason680    时间: 2014-03-14 17:09
本帖最后由 jason680 于 2014-03-14 17:10 编辑

回复 1# fubzio

脚本写死了 ==>改脚本

write another code to modify source
作者: fubzio    时间: 2014-03-14 17:18
to jason680: thanks

the problem is I have no idea about perl, who can help me write another code to modify source ?
作者: jason680    时间: 2014-03-14 17:50
本帖最后由 jason680 于 2014-03-14 17:55 编辑

回复 3# fubzio

There is simple way to do that
# OLD.pl is your source

$ ./ch_message_id.pl OLD.pl > NEW.pl


$ cat ch_message_id.pl
#!/usr/bin/perl

use strict;
use warnings;

my $sMessage_id = "";

while(<>){
  #message_id=>'123456789',
  if(m/message_id=>'(123456789)',/){
     if($sMessage_id eq ""){
       $sMessage_id = $1;
     }
     else{
       $sMessage_id++;
     }
     s/message_id=>'123456789',/message_id=>'$sMessage_id',/;
  }
  print;

}
   
作者: fubzio    时间: 2014-03-17 10:33
to jason680: 非常感谢!!!

我们先试试




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2