Using Perl to parse a CSV file from a specific line to the end of a file

I am very new to Perl and need your help

I have a CSV file xyz.csvwith the contents:

here level1 and er values โ€‹โ€‹are line names ... not numbers ...

level1,er
level2,er2
level3,er3
level4,er4

I will analyze this CSV file using the script below and pass the fields to the array in the first run

open(my $d, '<', $file) or die "Could not open '$file' $!\n";
while (my $line = <$d>) {
  chomp $line; 
  my @data = split "," , $line; 
  @XYX = ( [ "$data[0]", "$data[1]" ], );
}

For the second run, I take input from the command line and save it in a variable $val. My program should parse the CSV file from the value stored in the variable until it reaches the end of the file

for instance

I enter level2, so I need a script to parse the second line to the end of the CSV file, ignoring the values level2in the file and passing those values โ€‹โ€‹( level2to level4) to@XYX = (["$data[1]","$data[1]"],);}

level2,er2
level3,er3
level4,er4

level3, script CSV, level3 (level3 level4) @XYX = (["$data[0]","$data[1]"],);}

level3,er3
level4,er4

? , . .

+5
4
#!/usr/bin/perl  
use strict;     
use warnings;
open(my $data, '<', $file) or die "Could not open '$file' $!\n"; 
my $level = shift ||"level1"; 
while (my $line = <$data>) {  
chomp $line; 
my @fields = split "," , $line; 
if($fields[0] eq $level .. 0){
print "\n$fields[0]\n";
print "$fields[1]\n";
}}

... ...

0

, , , split. ,

. , , use strict use warnings Perl. , , , .

, "\n" die, Perl , . , ,

, Perl . @XYX $W !

, , , , . , @XYX = (["$data[1]","$data[1]"],). , $data[1] ? -, , - , , , , . , push @XYX ? , , .

$level_num . , $min_level, ,

use strict;
use warnings;

my $file = 'xyz.csv';
my $min_level = 3;
my @list;

open my $fh, '<', $file or die "Could not open '$file' $!";

while (my $line = <$fh>) {
  chomp $line; 
  my ($level, $error) = split ',', $line, 2;
  my ($level_num) = $level =~ /(\d+)/;
  next unless $level_num >= $min_level;
  push @list, [ $level, $error ];
}
+4

, , "" (..) .

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

my $level = shift || 'level1';

while (<DATA>) {
  if (/^\Q$level,/ .. 0) {
    print;
  }
}

__DATA__
level1,er
level2,er2
level3,er3
level4,er4

false, . false, ; false.

, , , . , /^\Q$level,/ ( $level ), ( , ).

I also highly recommend not parsing CSV records with split /,/. This may work with your current data, but as a rule, the fields in the CSV file may contain embedded commas that violate this approach. Instead, look at Text :: CSV or Text :: ParseWords (which is included in the standard Perl distribution).

Update: I seem to have a few downvotes. It would be great if people took the time to explain why.

+1
source
#!/usr/bin/perl

use strict;
use warnings;
use Text::CSV;

my @XYZ;
my $file = 'xyz.csv';
open my $fh, '<', $file or die "$file: $!\n";

my $level = shift; # get level from commandline
my $getall = not defined $level; # true if level not given on commandline

my $parser = Text::CSV->new({ binary => 1 }); # object for parsing lines of CSV

while (my $row = $parser->getline($fh)) # $row is an array reference containing cells from a line of CSV
{
  if ($getall # if level was not given on commandline, then put all rows into @XYZ
      or      # if level *was* given on commandline, then...
      $row->[0] eq $level .. 0 # ...wait until the first cell in a row equals $level, then put that row and all subsequent rows into @XYZ
     )
  {
    push @XYZ, $row;
  }
}

close $fh;
+1
source

All Articles