1

我得到 的输出 git show-ref --tags,如下所示:

1231243424324234234324242421412414121212 Tagname
1231341253432148224235423652423654782363 tagnametwo
2453491533961372624623247862387746223647 tagnamethree

我想将这些值放在哈希中,这样提交 id 是键,标签名称是它的值。

4

2 回答 2

2

Since your incoming data is arranged as alternating keys and assoicated values, all you need to do is extract that list and assign it to a hash. The important thing is to get the patterns right.

Defining the Patterns

Given that:

  • a git id is a sequence of hex digits matched by the pattern \p{ahex}+
  • its tag is a sequence of non-whitespace matched by the pattern \S+

You could do this to build up a hash mapping ids to tags:

my %id2tag = ();
my $data = `git show-ref --tags`;
while ( $data =~ / (\p{ASCII Hex Digit}+) \s+ (\S+) /gx ) {
    $id2tag{ $1 } = $2;
}

Named Groups

If you prefer named groups, support in Perl v5.10 or better, you could do it this way:

use v5.10;
my %id2tag = ();
my $data = `git show-ref --tags`;
while ( $data =~ / (?<ID> \p{ASCII Hex Digit}+ ) \s+ (?<TAG> \S+ ) /gx ) {
    $id2tag{ $+{ID} } = $+{TAG};
}

Interpolated Patterns

Or you could store the pattern in variables and interpolate them:

use v5.10;
my %id2tag = ();

my $id_rx  = qr/ (?<ID>  \p{ASCII Hex Digit} + ) /x;
my $tag_rx = qr/ (?<TAG> \S                  + ) /x;

my $data = `git show-ref --tags`;
while ( $data =~ / $id_rx \s+ $tag_rx /gx ) {
    $id2tag{ $+{ID} } = $+{TAG};
}

That approach is more extensible, and for more complex patterns, more readable.

Hash Assignment

Or you could do it all at once, since everything is already in the right order:

%id2tag = `git show-ref --tags` =~ / (\p{ASCII Hex Digit}+) \s+ (\S+) /gx;

A Real-world Demo

Here is an example of running this on some canned data I extracted from the current git repo for Perl itself.

my $data   = do { local $/; <DATA> };
my %id2tag = $data =~ /(\p{ahex}+)\s+(\S+)/gx;

for my $id (sort keys %id2tag) {
    print "$id $id2tag{$id}\n";
}
__END__
da71581858ddfe0f74d9e276c5bbe888c75b6d7f refs/tags/GitLive-blead
c4fb1312746aed9e0b696326f3c2664a71284324 refs/tags/GitLive-maint-5.004
5c487ee4c7541fbfd48376965aaefc58ee92541e refs/tags/GitLive-maint-5.005
acb6b050e21c7f3a48affc353ed066777fc1bd0a refs/tags/GitLive-maint-5.10
e03dffa8c006bd53504464a288371e71419497fd refs/tags/GitLive-maint-5.6
9b4fcc40a6ef2b8882b7dda7568ac911f7718b0f refs/tags/GitLive-maint-5.8
790fbd2b1de809897ebacddb828652a3f1d75dd9 refs/tags/code-review/2009-07-22
8d063cd8450e59ea1c611a2f4f5a21059a2804f1 refs/tags/perl-1.0
112e33b1b18999ffceafcdafac4e1888f882dc74 refs/tags/perl-1.0.15
4f78c20b5e222c45328d0f7f30988dc4bbe99c1e refs/tags/perl-1.0.16
378cc40b38293ffc7298c6a7ed3cd740ad79be52 refs/tags/perl-2.0
ffd30a0b488495f48bc676c58309803860e1e715 refs/tags/perl-2.001
a687059cbaf2c6fdccb5e0fae2aee80ec15625a8 refs/tags/perl-3.000
27e2fb84680b9cc1db17238d5bf10b97626f477f refs/tags/perl-3.044
fe14fcc35f78a371a174a1d14256c2f35ae4262b refs/tags/perl-4.0.00
e334a159a5616cab575044bafaf68f75b7bb3a16 refs/tags/perl-4.0.36
a0d0e21ea6ea90a22318550944fe6cb09ae10cda refs/tags/perl-5.000
fec02dd38faf8f83471b031857d89cb76fea1ca0 refs/tags/perl-5.000o
748a93069b3d16374a9859d1456065dd3ae11394 refs/tags/perl-5.001
8e07c86ebc651fe92eb7e3b25f801f57cfb8dd6f refs/tags/perl-5.001n
a5ebd2dff0239f64f342318d01cd92dc19aa52c8 refs/tags/v5.13.9
6a69229c1ab56c42c6097e1a84993663bd6a23f3 refs/tags/v5.14.0
6ac2c00aba1f90aa074905caff75d24ac4224c79 refs/tags/v5.14.0-RC1
a944cabb82c555112be417b6fdcf2abeea9e2c90 refs/tags/v5.14.0-RC2
3a178b6f9ebcc27e659c48b690758c679fec5cc5 refs/tags/v5.14.0-RC3
cf28be8222b700a410ab05a0d7a770e029973b0c refs/tags/v5.14.1
0f687828d8e355bb557d0cf0d3b274cf08f6bae7 refs/tags/v5.14.1-RC1
6a56b9d83500deafe8d850e244906f5513cdd222 refs/tags/v5.14.2
8b560c3170c9c6e2263384868d0287017aecb59d refs/tags/v5.14.2-RC1
cc2b21d47cd66b31fa33901d19112160488dc7d7 refs/tags/v5.15.0
cbeacc71ada41500c888d4cbd36c7314a14843d1 refs/tags/v5.15.1
a818eb37604096e91a51fbb947af5754c7067235 refs/tags/v5.15.2
eaa96143a800c6dca466021db99e8fe857e9e1b1 refs/tags/v5.15.3
5f4091681272a7ec88e3fe7eb014a8083ed95c5f refs/tags/v5.15.4
424c2e57d9f2f65dc08ae9e0d8179f9aa5d581d9 refs/tags/v5.15.5
33a408b7de68c683fad755d341d7899ec1953216 refs/tags/v5.15.6
c605ae044d8a52e820eb785305f0fd4d271faf51 refs/tags/v5.15.7
9b30811fee3478af913382334909acde68afd36d refs/tags/v5.15.8
7b50a15ad488ff4d4f50c99b5e534ede59c2d4af refs/tags/v5.15.9
fb2f5b1704c64fe55da0a05bbc71480c98fe5aac refs/tags/v5.16.0
340c15b2c5103c00d84cd7f15a65ca6ad15a116d refs/tags/v5.16.0-RC1
adee78d52422370b60888092a0155e40cdade038 refs/tags/v5.16.0-RC2
b59ea6d0dab824ba97b3cd1cfc85b3d6e91aab63 refs/tags/v5.16.1
aca8d19feb348b88fde0b018584caa2f40790d69 refs/tags/v5.17.0
002fcd4de39f0355cb9158aca59103d1218836a5 refs/tags/v5.17.1
7ef87a62992c4ed1cad491f65a28ee406fe909e7 refs/tags/v5.17.2
1e37de6eef7be989cb0181b094452e0adbefe976 refs/tags/v5.17.3

Prints out things like:

002fcd4de39f0355cb9158aca59103d1218836a5 refs/tags/v5.17.1
0f687828d8e355bb557d0cf0d3b274cf08f6bae7 refs/tags/v5.14.1-RC1
112e33b1b18999ffceafcdafac4e1888f882dc74 refs/tags/perl-1.0.15
1e37de6eef7be989cb0181b094452e0adbefe976 refs/tags/v5.17.3
27e2fb84680b9cc1db17238d5bf10b97626f477f refs/tags/perl-3.044
33a408b7de68c683fad755d341d7899ec1953216 refs/tags/v5.15.6
340c15b2c5103c00d84cd7f15a65ca6ad15a116d refs/tags/v5.16.0-RC1
378cc40b38293ffc7298c6a7ed3cd740ad79be52 refs/tags/perl-2.0
于 2012-09-15T21:59:28.853 回答
0

1) Get the output of the command

my $output = qx/git show-ref --tags/;

2) Write a regular expresion and iterate on it:

my @RESULT;
while( $output =~ m!(\w+)\s*!g ) {
   push @RESULT, $1;
}

3) Build your hash from array

my %MY_HASH = @RESULT;
于 2012-09-15T21:36:26.530 回答