Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 格式化数组元素_Perl - Fatal编程技术网

Perl 格式化数组元素

Perl 格式化数组元素,perl,Perl,嗨,我有一个数组,看起来像 @array = ( "city: chicago", "city: Newyork", "city: london", "country: india", "country: england", "country: USA") 我希望阵列看起来像: @array = ("city:", "chichago","Newyork","london","country:","india","england","USA") 有人能帮我把数组的格式设置成如下格式吗。用空格

嗨,我有一个数组,看起来像

@array = ( "city: chicago", "city: Newyork", "city: london", "country: india", "country: england", "country: USA")
我希望阵列看起来像:

@array = ("city:", "chichago","Newyork","london","country:","india","england","USA")

有人能帮我把数组的格式设置成如下格式吗。

用空格分割数组的每个元素,如果已经看到
城市:
国家:
字符串,它会跳过它们,否则会将它们与城市或国家名称一起映射为新元素

my @array = ("city: chicago", "city: Newyork", "city: london", "country: india", "country: england", "country: USA");
my %seenp;
@array = map {
  my ($k,$v) = split /\s+/, $_, 2;
  $seenp{$k}++ ? $v : ($k,$v);
}
@array;

将数组中的每个元素用空格分隔,如果已看到
城市:
国家:
字符串,则跳过它们,否则将它们与城市或国家名称一起映射为新元素

my @array = ("city: chicago", "city: Newyork", "city: london", "country: india", "country: england", "country: USA");
my %seenp;
@array = map {
  my ($k,$v) = split /\s+/, $_, 2;
  $seenp{$k}++ ? $v : ($k,$v);
}
@array;

为什么要把事情分开,然后把它们塞回一个难以使用的结构中呢。一旦你把他们分开,就让他们分开。用这种方法工作起来容易多了

#!/usr/bin/env perl

use strict;
use warnings;

# --------------------------------------

use charnames qw( :full :short   );
use English   qw( -no_match_vars );  # Avoids regex performance penalty

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

# conditional compile DEBUGging statements
# See http://lookatperl.blogspot.ca/2013/07/a-look-at-conditional-compiling-of.html
use constant DEBUG => $ENV{DEBUG};

# --------------------------------------


my @array = ( "city: chicago", "city: Newyork", "city: london", "country: india", "country: england", "country: USA");
my %hash = ();
for my $item ( @array ){
  my ( $key, $value ) = split m{ \s+ }msx, $item;
  push @{ $hash{$key} }, $value;
}

print Dumper \%hash;

为什么要把事情分开,然后把它们塞回一个难以使用的结构中呢。一旦你把他们分开,就让他们分开。用这种方法工作起来容易多了

#!/usr/bin/env perl

use strict;
use warnings;

# --------------------------------------

use charnames qw( :full :short   );
use English   qw( -no_match_vars );  # Avoids regex performance penalty

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

# conditional compile DEBUGging statements
# See http://lookatperl.blogspot.ca/2013/07/a-look-at-conditional-compiling-of.html
use constant DEBUG => $ENV{DEBUG};

# --------------------------------------


my @array = ( "city: chicago", "city: Newyork", "city: london", "country: india", "country: england", "country: USA");
my %hash = ();
for my $item ( @array ){
  my ( $key, $value ) = split m{ \s+ }msx, $item;
  push @{ $hash{$key} }, $value;
}

print Dumper \%hash;

如果数组是my@array=(“城市:芝加哥”、“城市:纽约”、“城市:伦敦”、“国家:印度”、“国家:英格兰德国”、“国家:美国加利福尼亚”);在本例中,它只打印第一部分,即,在名称(英格兰-德国)之前,它只打印英格兰而不是“英格兰-德国”。这就是
split
的工作原理。要得到“英格兰-德国”,你必须改变
my($k,$v)=split类似于
my($k,$v)=split/\s+/,$\u2。这将
拆分限制为仅生成两个组。如果数组为my@array=(“城市:芝加哥”、“城市:纽约”、“城市:伦敦”、“国家:印度”、“国家:英格兰-德国”、“国家:美国-加利福尼亚”);在本例中,它只打印第一部分,即,在名称(英格兰-德国)之前,它只打印英格兰而不是“英格兰-德国”。这就是
split
的工作原理。要得到“英格兰-德国”,你必须改变
my($k,$v)=split类似于
my($k,$v)=split/\s+/,$\u2。这将
拆分限制为只生成两个组。