在Perl中以空格分隔

在Perl中以空格分隔,perl,Perl,我有一个数据类型- 500 3.6673656 ---------- 1000 3.2707536 ---------- 1500 3.2356145 ---------- 2000 3.0495141 ---------- 2500 3.016674 i、 时间和距离。 我需要在一个数组中分割时间,在另一个数组中分割距离。 使用my@line=split(/\s+/,$)我可以在一个数组中存储距离,但不能存储时间。 是否有其他方法将它们分别存储在不同的阵列中 输入取自文件,内容存储在@ar

我有一个数据类型-

500 3.6673656 
----------
1000 3.2707536
----------
1500 3.2356145
----------
2000 3.0495141
----------
2500 3.016674
i、 时间和距离。 我需要在一个数组中分割时间,在另一个数组中分割距离。 使用
my@line=split(/\s+/,$)我可以在一个数组中存储距离,但不能存储时间。
是否有其他方法将它们分别存储在不同的阵列中

输入取自文件,内容存储在
@array

我的剧本:

    foreach $_ (@array){ 
    if($_ =~ /[@]/) {# do nothing, it's a comment or formatting line} 
    else  {my @line = split( /\s+/, $_);
    print "@line\n";}
}

请看一看这个例子:

use strict;
use warnings;
use Data::Dumper;

my $str=<<EOF;
500 3.6673656
1000 3.2707536
1500 3.2356145
2000 3.0495141
2500 3.016674
EOF

my @arr= split /\n/, $str;
my @arr1;
my @arr2;

foreach (@arr) {
    chomp;
    my $line = $_;
    next if ($line=~/^\s*$/);
    my ($val1,$val2) = $line=~/(\S+)\s+(\S+)/;
    push @arr1,$val1;
    push @arr2, $val2;
}

print Dumper (\@arr1);
print Dumper (\@arr2);
使用严格;
使用警告;
使用数据::转储程序;
我的$str=你可以做:

my (@times, @dists);
foreach (@array) { 
    if (/[@]/) {
        # do nothing, it's a comment or formatting line
    } 
    else  {
        my ($time, $dist) = split( /\s+/, $_);
        push @times, $time;
        push @dists, $dist;
    }
}

您可以使用之前的
split
命令。只需将结果保存在两个变量中,然后
将其推送到两个不同的数组中即可

my (@times, @distances);
foreach my $line (@array) {
  next if $line eq '----------'; # skip lines with lines (no pun intended)
  my ($t, $d) = split /\s/, $line;
  push @times, $t;
  push @distances, $d;
}

拆分行和存储数据是很简单的。您想如何存储它以供以后使用是个问题。可以在空格上拆分线,并将其存储为:

my @data =  map { [ split ] } @lines;
split
的默认调用会在空格上拆分
$\uu

接下来,您可以使用grep省略带有
'@'
的行:

my @data = map { [ split ] } grep { index( $_, '@' ) == -1 } @lines;
下面是最简单的存储结构:

use strict;
use warnings;
use constant TIME     => 0;
use constant DISTANCE => 1;

my @data = map { [ split ] } grep { index( $_, '@' ) == -1 } @lines;
然后,您可以通过不同字段的插槽名称对其进行寻址

foreach my $row ( @data ) {
    printf "At time : %d, distance was %f\n", $row->[TIME], $row->[DISTANCE];
}

做你认为你想做的事很容易

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

my (@distance, @time);

while (<DATA>) {
  next if /----/;
  chomp;

  my ($t, $d) = split; # Splits on whitespace by default
  push @distance, $d;
  push @time, $t;
}

say "@distance";
say "@time";

__DATA__
500 3.6673656 
----------
1000 3.2707536
----------
1500 3.2356145
----------
2000 3.0495141
----------
2500 3.016674
#/usr/bin/perl
严格使用;
使用警告;
使用5.010;
我的(@距离,@时间);
而(){
下一个if/---/;
咀嚼;
my($t,$d)=split;#默认情况下在空格上拆分
按距离,$d;
推送@time,$t;
}
说“距离”;
说“@时间”;
__资料__
500 3.6673656 
----------
1000 3.2707536
----------
1500 3.2356145
----------
2000 3.0495141
----------
2500 3.016674
然而,这是一个非常糟糕的主意。您输入的每一行上的距离和时间显然是相互关联的。因此,将它们存储在单独的变量中是个坏主意,因为这两个值之间的关系表示为它们在不同数组中具有相同的索引

my (@times, @distances);
foreach my $line (@array) {
  next if $line eq '----------'; # skip lines with lines (no pun intended)
  my ($t, $d) = split /\s/, $line;
  push @times, $t;
  push @distances, $d;
}
一种更好的方法是将这两个值存储在一起(可能在散列中)并存储这些散列(或者更准确地说,在数组中存储对这些散列的引用)

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

use Data::Dumper;

my (@data);

while (<DATA>) {
  next if /----/;
  chomp;

  my %row;

  @row{qw[time distance]} = split; # Splits on whitespace by default
  push @data, \%row;
}

say Dumper \@data;

__DATA__
500 3.6673656 
----------
1000 3.2707536
----------
1500 3.2356145
----------
2000 3.0495141
----------
2500 3.016674
!/usr/bin/perl
严格使用;
使用警告;
使用5.010;
使用数据::转储程序;
我的(@数据);
而(){
下一个if/---/;
咀嚼;
我的%行;
@行{qw[time-distance]}=split;#默认情况下在空白处拆分
推送@数据,\%行;
}
说Dumper\@数据;
__资料__
500 3.6673656 
----------
1000 3.2707536
----------
1500 3.2356145
----------
2000 3.0495141
----------
2500 3.016674

让我们穿上所有漂亮的裤子:

#! /usr/bin/env perl
#

use strict;
use warnings;
use feature qw(say);

my @array;
while ( my $line = <DATA> ) {
    chomp $line;
    push @array, $line;
}

#
# As two separate arrays (Not so good)
#
my @times;
my @distances;
for my $entry ( @array ) {
    chomp $entry;               # Not needed, but never hurts
    next if $entry =~ /--+$/;   # Next if all dashes
    my ( $distance, $time ) = split /\s+/, $entry;
    push @times, $time;
    push @distances, $distance;
}
say "The first entry as two distinct arrays";
say "Distance: $distances[0]";
say "Time: $times[0]";

#
# As two entries in a single array
#
my @velocities;
for my $entry ( @array ) {
    chomp $entry;               # Not needed, but never hurts
    next if $entry =~ /--+$/;   # Next if all dashes
    my @velocity = split /\s+/, $entry;
    push @velocities, \@velocity;
}
say "The first entry as an array of arrays";
say "Distance: " . $velocities[0]->[0];
say "Time: " . $velocities[0]->[1];
#
# As a hash in an array (Better Still)
# Note: Using regular expression to split
#
my @velocities2;
for my $entry ( @array ) {
    chomp $entry;               # Not needed, but never hurts
    next unless $entry =~ /\s*(\S+)\s+(\S+)/;
    my %velocity;
    $velocity{DISTANCE} = $1;
    $velocity{TIME} = $2;
    push @velocities2, \%velocity;
}
say "The first entry as an array of hashes";
say "Distance: " . $velocities2[0]->{DISTANCE};
say "Time: " . $velocities2[0]->{TIME};
#
# As objects (The best!)
#
my @velocities3;
for my $entry ( @array ) {
    chomp $entry;               # Not needed, but never hurts
    next unless $entry =~ /\s*(\S+)\s+(\S+)/;
    my $distance = $1;
    my $time = $2;
    my $velocity = Local::Velocity->new( $distance, $time );
    push @velocities3, $velocity;
}
say "The first entry as an object";
say "Distance: " . $velocities3[0]->distance;
say "Time: " . $velocities3[0]->time;

package Local::Velocity;

sub new {
    my $class    = shift;
    my $distance = shift;
    my $time     = shift;

    my $self = {};
    bless $self, $class;
    $self->distance( $distance );
    $self->time( $time );
    return $self;
}

sub distance {
    my $self     = shift;
    my $distance = shift;

    if ( defined $distance ) {
        $self->{DISTANCE} = $distance;
    }
    return $self->{DISTANCE};
}

sub time {
    my $self    = shift;
    my $time    = shift;

    if ( defined $time ) {
        $self->{TIME} = $time;
    }
    return $self->{TIME};
}

package main;
__DATA__
500 3.6673656 
----------
1000 3.2707536
----------
1500 3.2356145
----------
2000 3.0495141
----------
2500 3.016674
!/usr/bin/env perl
#
严格使用;
使用警告;
使用特征qw(例如);
我的@数组;
while(我的$line=){
chomp$行;
按@数组,$line;
}
#
#作为两个独立的阵列(不太好)
#
我的@times;
我的距离;
对于我的$entry(@array){
chomp$entry;#不需要,但不会受伤
下一个if$entry=~/--+$/;#下一个if所有破折号
my($distance,$time)=拆分/\s+/,$entry;
推@次,$time;
按@距离,$distance;
}
说“第一个条目作为两个不同的数组”;
说“距离:$Distance[0]”;
说“时间:$times[0]”;
#
#作为单个数组中的两个条目
#
我的速度;
对于我的$entry(@array){
chomp$entry;#不需要,但不会受伤
下一个if$entry=~/--+$/;#下一个if所有破折号
my@velocity=split/\s+/,$entry;
推动@速度,\@速度;
}
说“作为数组数组的第一个条目”;
说“距离:”.$speeds[0]->[0];
说“时间:.”速度[0]->[1];
#
#作为数组中的散列(更好)
#注意:使用正则表达式进行拆分
#
我的@velocities2;
对于我的$entry(@array){
chomp$entry;#不需要,但不会受伤
除非$entry=~/\s*(\s+)\s+(\s+)/;
我的%速度;
$velocity{DISTANCE}=$1;
$velocity{TIME}=$2;
按@速度2,\%速度;
}
说“作为散列数组的第一个条目”;
说出“距离:.$velocies2[0]>{Distance};
说“时间:.$velocies2[0]>{Time};
#
#作为对象(最好!)
#
我的@velocities3;
对于我的$entry(@array){
chomp$entry;#不需要,但不会受伤
除非$entry=~/\s*(\s+)\s+(\s+)/;
我的$distance=$1;
我的$time=$2;
my$velocity=Local::velocity->new($distance,$time);
推@velocities3,$velocity;
}
说“作为对象的第一个条目”;
说出“距离:”.$Velocies3[0]->距离;
说“时间:”.$Velocies3[0]->时间;
包本地::速度;
次新{
我的$class=shift;
我的$distance=shift;
我的$time=shift;
我的$self={};
祝福$self,$class;
$self->distance($distance);
$self->time($time);
返回$self;
}
子距离{
我的$self=shift;
我的$distance=shift;
如果(定义的$距离){
$self->{DISTANCE}=$DISTANCE;
}
返回$self->{DISTANCE};
}
分时{
我的$self=shift;
我的$time=shift;
如果(定义为$time){
$self->{TIME}=$TIME;
}
返回$self->{TIME};
}
主包装;
__资料__
500 3.6673656 
----------
1000 3.2707536
----------
1500 3.2356145
----------
2000 3.0495141
----------
2500 3.016674
第一种方法是您所要求的:两个并行数组。这种方法的问题是,您现在被迫保持两个独立的数据结构的顺序。如果您传递时间和距离,您必须传递两个独立的数据元素。如果您修改一个,您必须修改另一个。如果您从一个
推送
弹出
,您必须我想对另一个人做这件事

只有两个还不算太坏,但想象一下,要用一打或更多的来做这件事

第二种方法使用。引用允许您执行更复杂的数据结构。这将两个条目保持在一个数组中。现在,您有一个数组包含两个条目。
push
one,您
push
另一个。
pop
一个,您
pop
另一个。如果您将时间和距离传递给su布鲁丁,你只需要通过一个条目

<