Split into spaces in Perl

I have data like -

500 3.6673656 
----------
1000 3.2707536
----------
1500 3.2356145
----------
2000 3.0495141
----------
2500 3.016674

i.e. time and distance. I need to split time in one array and distance in another. using my @line = split( /\s+/, $_);, I could store the distance in one array, but I cannot save time. Is there any other way to separately store each of them in different arrays?

Enter

taken from the file, and the contents are stored in @array.

my script:

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

Let me get all the fashion pants:

#! /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

- , : . , . , . , . push pop , .

, , .

. . . , . push , push . pop , pop . , .

. . , . ? , DISTANCE. , , . , , .., .

- . , , . . Local::Velocity, .

, :

  • , DISTANCE, DISTANCE DISTANCE, distanse. DISTANCE. , , .
  • , . , velocity, . , , . .

- Perl , , . .

+6

. , . :

my @data =  map { [ split ] } @lines;

split $_ .

'@' 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];
}
+5

:

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

split, . push .

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;
}
+2

, , , .

#!/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

However, this is a really bad idea. The distance and time on each line of your input is obviously related to each other. Therefore, it is nice to store them in separate variables, where the relationship between these two values โ€‹โ€‹is represented by the fact that they both have the same index in different arrays.

It would be best to store the two values โ€‹โ€‹together (possibly in a hash) and store these hashes (or, more precisely, the references to these hashes in an array.

#!/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
+2
source

Please take a look at this example:

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);
+1
source

All Articles