Have you declared all your variables?
Are all your subroutines defined?
Are your variables properly scoped?
Have you checked for anything redundant?
Is any part of your code unreachable?
(Can it be removed?)
Third, run some checks to see how bad the damage is:).Name: /root/Desktop/rework.pl Version: v2.0.0 Directory: File: /root/Desktop/rework.pl Core module: no Modules used: English File::Spec Getopt::Std IO::File Switch Time::HiRes autodie strict version warnings Packages created: main 1312019949.17447 Subroutines defined: main assign_headers assign_names check_params get_fields_plan get_params trim
Here's your script as I fixed it, minus the file part:perl -c script.pl perl -w script.pl perl -MO=Lint script.pl perltidy script.pl
#!/usr/bin/perl use strict; use warnings; use File::Spec; use Getopt::Std; use IO::File; use Switch; use Time::HiRes qw(gettimeofday tv_interval); use version 0.77; our $VERSION = qv("v2.0.0"); use English qw(-no_match_vars); local $OUTPUT_AUTOFLUSH = 1; print my $timer_start = gettimeofday(), "\n"; my(%inputs) = (); my(%params) = (); getopts('o:r:p:t:n:f:g:d', \%inputs); die "Error Getting Parameters\n" unless get_params(\%inputs, \%params); check_params(\%params); my $outfile = File::Spec->catfile($params{'outfolder'},$params{'rootname'}); my $outnamefile = File::Spec->catfile($params{'outfolder'}, $params{'rootname'} ); my $outlinecount = $outfile . '_db_proc_linecount.txt'; $outfile = $outfile . '_db_proc_data.csv'; $outnamefile = $outnamefile . '_db_proc_names.csv'; print "Output SSS File: $outfile\n"; print "Output Names File: $outnamefile\n"; my $hdrmap; my(%hdr_types) = ( fields =>\&get_fields_area, regions =>\&get_fields_region, plan =>\&get_fields_plan ); die 'Unable to match sss type. ' unless defined($hdr_types{$params{ssstype}}); $hdrmap = $hdr_types{$params{ssstype}}(); my $namemap = get_fields_plan($params{ssstype}); #open I, '<', $infile or die "Unable to open file $infile.\n"; open my $O, '>', $outfile or die "Unable to open file $outfile.\n"; open my $ON, '>', $outnamefile or die "Unable to open file $outnamefil +e.\n"; #open OLCT, '<', $outlinecount or die "Unable to open file $outlinecou +nt.\n"; foreach (my $i=0; $i<3; $i++) { my $line = <$O>; chomp ($line); $line =~ s/^\s+//; $line =~ s/\s+$//; my @sp = split (/\s+/,$line); foreach (my $j=0; $j<scalar(@sp); $j++) { my @hdrs; @hdrs = $hdrs[$j] . q{} . $sp[$j]; } } print "[" . join(",", my @hdrs) . "]\n"; foreach my $hdr (@hdrs){ trim(\$hdr); print "CHECKING HEADERS ------------\n"; } foreach my $key (sort keys %{$hdrmap}) { my ( $index )= grep { $hdrs[$_] eq $hdrmap->{$key}[0] } 0..$#hdrs +; if (defined ($index)) { $hdrmap->{$key}[1] = $index; print "HEADER MATCH: " . "\t" . $key . "\t" . $hdrmap->{$key +}[0] . "\t[" . $hdrmap->{$key}[1]. "]\n"; } else { print "HEADER NOT FOUND: " . "\t" . $key . "\t" . $hdrmap->{ +$key}[0] . "\n"; } } print "-----------------------------------------------\n"; my @flist = (); my @fnamelist = (); assign_names($hdrmap, \@flist); assign_names($namemap, \@fnamelist); print "[" . join(",", @flist) . "]\n"; print $O join(",", @flist) . ",RUN_ID\n"; print $ON join(",", @fnamelist) . ",RUN_ID\n"; exit(); my $l_units = <$O>; chomp ($l_units); print "UNITS -- > [$l_units]\n"; use autodie qw(:close); close($O); close($ON); sub get_params { my $inr = shift; my $pr = shift; $pr->{'debug'} = 1; $pr->{'runid'} = -1000; $pr->{'filter'} = ".*"; $pr->{'datefilter'} = ""; $pr->{'filename'} = "NA"; $pr->{'outfolder'} = "."; $pr->{'ssstype'} = ""; foreach my $key (keys %{$inr}) { $pr->{'debug'} = $inr->{$key} if $key eq 'd'; $pr->{'runid'} = $inr->{$key} if $key eq 'n'; $pr->{'filter'} = $inr->{$key} if $key eq 'f'; $pr->{'datefilter'} = $inr->{$key} if $key eq 'g'; $pr->{'filename'} = $inr->{$key} if $key eq 'r'; $pr->{'outfolder'} = $inr->{$key} if $key eq 'o'; $pr->{'ssstype'} = $inr->{$key} if $key eq 't'; } return 1; } sub check_params { my $pr = shift; die "Require RunID if not in debug mode\n" if ($pr->{debug} == 1 a +nd $pr->{runid} == -1000); (-e $pr->{filename}) or die "Unable to find input filename: $pr->{ +filename}\n"; (-e $pr->{outfolder}) or die "Unable to find outfolder: $pr->{outf +older}\n"; $pr->{filter} = ".*" if $pr->{filter} eq ""; my ($volume, $dirs, $rootname) = File::Spec->splitpath($params{fil +ename}); $rootname =~ s/\.sss$//; my @sp = split(/_/,$rootname); $params{rootname} = $rootname; $params{ssstype} = $sp[-1] if $params{ssstype} eq ""; print "Run Parameters: \n"; foreach my $key (keys %{$pr}) { print "$key -> [$pr->{$key}]\n"; } return 1; } sub get_fields_plan { my $tblFields = shift; $tblFields->{'DATE_EN'} = ["__________ __________ Date",-1]; $tblFields->{'HEADER1'} = ["Header1 Header1 Header1",-1]; $tblFields->{'HEADER2'} = ["Header2 Header2 Header2",-1]; $tblFields->{'HEADER3'} = ["Header3 Header3 Header3",-1]; $tblFields->{'HEADER4'} = ["Header4 Header4 Header4",-1]; $tblFields->{'HEADER5'} = ["Header4 Header5 Header5",-1]; $tblFields->{'HEADER6'} = ["Header6 Header6 Header6",-1]; return $tblFields; } sub assign_headers { my $hdrmap = shift; my $headline = shift; my $sptagref = shift; $$sptagref = '\s*,\s*' if $$headline=~ m/,/; my @hdrs = split(/$$sptagref/, $$headline); foreach my $hdr (@hdrs){ trim(\$hdr); print "CHECKING HEADERS ------------\n"; } foreach my $key (sort keys %{$hdrmap}) { my ( $index )= grep { $hdrs[$_] eq $hdrmap->{$key}[0] } 0. +.$#hdrs; if (defined ($index)) { $hdrmap->{$key}[1] = $index; print "HEADER MATCH: " . "\t" . $key . "\t" . $h +drmap->{$key}[0] . "\t[" . $hdrmap->{$key}[1]. "]\n"; } else { print "HEADER NOT FOUND: " . "\t" . $key . "\t" . $hdrma +p->{$key}[0] . "\n"; } } print "-----------------------------------------------\n"; } sub assign_names { my $hdrmap = shift; my $flist = shift; @$flist = (); foreach my $key (sort keys %{$hdrmap}) { my $i = $hdrmap->{$key}[1]; push(@{$flist}, $key) if ($i != -1); } } sub trim { my $sref = shift; $$sref =~ s/^\s+//; $$sref =~ s/\s+$//; }
In reply to Re: Space delimted to CSV, Index and data extraction loop
by Khen1950fx
in thread Space delimted to CSV, Index and data extraction loop
by SixShot
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |