0

まあ、アイデアは、ファイルをその説明とともに方向に削除し、それをハッシュに保存することです

これはファイル /home/opmeitle/files-pl/bookmarks2 のコンテンツです

    }, {
       "date_added": "12989744094664781",
       "id": "1721",
       "name": "Perl DBI - dbi.perl.org",
       "type": "url",
       "url": "http://dbi.perl.org/"
    }, {
       "date_added": "12989744373130384",
       "id": "1722",
       "name": "DBD::mysql - MySQL driver for the Perl5 Database Interface (DBI) - metacpan.org",
       "type": "url",
       "url": "https://metacpan.org/module/DBD::mysql"
    }, {

今、perlのコード。

use strict;

open(FILE, '/home/opmeitle/files-pl/bookmarks2');  
my @lines = <FILE>;
my @list55;
my $count = 1;
my $n = 0;
my %hash=();   #$hash{$lines[$n]}=$lines[$n];
    while ($lines[$n]) {
        if ($lines[$n] =~ /(http:|https:|name)/) {
            if ($lines[$n] =~ s/("|: |,|id|url|name|\n)//g) {
                if ($lines[$n] =~ s/^\s+//){
                    if ($lines[$n] =~ /http:|https/){ 
                        $hash{$lines[$n]} = '';
                    }
                    else {
                        $hash{$n} = $lines[$n];
                    }
                }
            }
        }
    $n++;
    $count++;
    }
close(FILE);
# print hash
my $key;
my $value;
while( ($key,$value) = each %hash){
    print "$key = $value\n";
}

スクリプト実行後の結果。

http://dbi.perl.org/ = 
https://metacpan.org/module/DBD::mysql = 
3 = Perl DBI - dbi.perl.org
9 = DBD::mysql - MySQL driver for the Perl5 Database Interface (DBI) - metacpan.org

しかし、私はこのようなものが必要です

http://dbi.perl.org/ = Perl DBI - dbi.perl.org
Perl DBI - dbi.perl.org = DBD::mysql - MySQL driver for the Perl5 Database Interface (DBI) - metacpan.org

答えてくれてありがとう。

4

2 に答える 2

2

@amon が示唆したように、Chrome ブックマークは JSON 形式であり、CPANにはいくつかの優れたモジュールがあります。

use strict;
use warnings;
use JSON;

my $file = '/home/opmeitle/files-pl/bookmarks2';
open my $fh, '<', $file or die "$file: $!\n";
my $inhash = decode_json(join '', <$fh>);
close $fh;

my %outhash = map traverse($_), values %{ $inhash->{roots} };
sub traverse
{
  my $hashref = shift;

  if (exists $hashref->{children}) {
    return map traverse($_), @{ $hashref->{children} };
  } else {
    return $hashref->{url} => $hashref->{name};
  }
}

%outhashこれで、必要なデータが得られました。

編集:ここで何が起こっているのかを理解するのを助けるために:

use Data::Dumper;
print Dumper($inhash); # pretty-print the structure returned by decode_json
于 2012-09-02T19:56:28.140 に答える
1

他の人が言ったように、最善の方法は JSON データを Perl データ構造にロードすることです。JSONこれは、モジュールを使用して簡単に実行できます。これを行う前に、ファイルを読み込む必要があります。これには 2 つの方法があります。非 CPAN の方法:

# always ...
use strict;
use warnings;

my $file = '/home/opmeitle/files-pl/bookmarks2';

my $text = do {
  open my $fh, '<', $file or die "Cannot open $file: $!\n";
  local $/; #enable slurp
  <$fh>;
};

またはCPANの方法

# always ...
use strict;
use warnings;

use File::Slurp;
my $text = read_file $file;

ファイルを読み込んだら、デコードします

use JSON;

my $data = decode_json $text;

ファイル全体と、必要なもののより良い説明を投稿してください。データ構造をトラバースするより正式な方法について喜んでコメントします。

于 2012-09-02T20:52:19.013 に答える