English 中文(简体)
序号/编码和修改数据
原标题:serialize/deserialize & modify data
  • 时间:2011-01-02 19:33:43
  •  标签:
  • perl

我有从一个数据库中提取序列化综合网址数据、将其进行航空、对数据进行模拟、然后重新编号的每字母。 我想做的是修改名称和插图;地毯(如下文所示),但可以说明如何进入各个田地加以修改:

use PHP::Serialization qw(serialize unserialize);
use Data::Dumper qw(Dumper); 

###blah, blah, blah
while ( @a = $sth->fetchrow() ){
my $hashref = unserialize( $a[0] );
print Dumper($hashref); 
}
OUTPUT:
$VAR1 = [
      bless( {
                name  =>  Fred , # I want this to be Dave
                pet  =>  Cat ,  # I want this to be Dog
                date  =>  1977 
             },  PHP::Serialization::Object::stdClass  ),
      bless( {
               name  =>  Mary , # I want this to be Jane
                pet  =>  Worm , # I want this to be Pig
                date  =>  1977 
             },  PHP::Serialization::Object::stdClass  )
    ];

UPDATE: Thx to Hugmeir, I have the following, which seems to work. Is this the best way to change the name if I don t know the index number?

for my $hashref (@{$array_ref}) {

        if ( $hashref->{name} =~ /Mary/ ){
          $hashref->{name} =  Jane ; 
         }

}
最佳回答

对于开端人来说,这并不是一种耳光——它包含两个元素的阵列,每个元素都包含在内。 这打破了PHP:Serialization s encapsulation,但应当做到:

my $array_ref = unserialize( $a[0] );

for my $hashref (@{$array_ref}) {
    @{$hashref}{qw(name pet)} = ( New name ,  New Pet );
    #Or $hashref->{name} =  new name ; If you don t like slices.
}

EDIT:如果你只想修改第一个要素,你可以这样做。

$array_ref->[0]->{name} =  etc ;

* 技术上有两个幼苗:

问题回答

暂无回答




相关问题
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 ...

热门标签