English 中文(简体)
hw 能够比较两个 perl hashes( 钥匙和值) 提取差异, 并应用到 javastrash 中的大麻
原标题:hw can i compare two perl hashes (keys and values) extract difference and apply to a hash in javascript

Cavinat,我希望我问的是正确的问题, 你可以随意建议其他选择。

Situation; I have a site written mainly in javascript that runs on fixed and mobile devices. The site is data heavy. It maps a load of moving objects and other interactions. Each client receives an update to the data from the server every few seconds, and so, I need to minimise the amount of data sent. Currently, the server sends a rather large hash containing the current state of all the objects on each update. This is very inefficient because much of the data remains unchanged.

我认为,对发送的数据量产生最大影响的办法,加上最少的编码和测试,是只将修改发送到每份散列更新中。

所以,我想知道,在将上次发送的数据(hash)与将要发送的数据(hash)进行比较,提取差异,然后将差异应用到印花上的大麻上,是否有任何经过试验和测试的方法?我希望这有意义吗?

当我说比较时,我的意思是关键和价值观。 目前,杂交不是以任何特定方式订购的,但是,如果需要的话,这不是一个问题。

我研究过数据:比较, 但是它似乎只是告诉我, 如果杂交是不同的, 而不是区别是什么(除非我看错了吗?) 。

最佳回答

我对从服务器向客户端发送不同对象( JSON 格式) 的问题也有同样的问题。 我们提出的解决方案非常简单。 由于没有官方或标准的方法来表达json 对象之间的差异, 我们想出了自己的协议, 如何定义添加的对象, 哪些是更新的对象( 即这些被修改( 添加/更新/ 删除) 对象的属性), 哪些是完全删除的对象 。

一种可能的三角洲有效载荷可能是:

{
  added: [
    /* array of new objects */
  ],
  removed: [
    /* array of object identifiers that need to be removed */
  ],
  updated: { /* key value pairs of object identifiers with their property maps */
    obj_id_01: {
      updated: { /* key-value pairs of updated properties */ },
      removed: [ /* array of keys of removed properties in an object */ ]
    },
    obj_id_02: {
    },
    ...
  }
}
问题回答

我为类似的问题制定了类似的解决办法。

my $bef = {
  name =>  Fred ,
  wife =>  Wilma ,
  hobby =>  Breaking Rocks ,
  friends => [qw! Barney Wilma Betty !],
};

my $aft = { 
  name =>  Fred ,
  pet =>  Dino ,
  hobby =>  Bowling ,
  friends => [qw! Barney Betty Dino !],
  kids => [qw! Bam Pebbles !],
};

my $differ = Lecstor::FeedProxy::Diff->new;

my $diff = $diff->differences($bef, $aft);

$diff: {
   pet  =>  Dino ,
   wife  => undef,
   hobby  =>  Bowling ,
   friends  => {
     remove  => [  Wilma  ],
     add  => [  Dino  ]
  },
   kids  => {
     add  => [  Bam ,  Pebbles  ]
  }
};

https://github.com/lecstor/Lecstor/blob/master/lib/Lecstor/FeedProxy/Diff.pm < /a>





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签