in reply to Re: Inline change to the file passed by Glob
in thread Inline change to the file passed by Glob

I want to further process two files so i wrote the code like this

use POSIX; use List::Util qw[min max]; use English; use Getopt::Long; use File::Basename; use autodie; my ($help,$csv_add, $csv_flex); GetOptions ( "csv_add=s" => \$csv_add, "csv_flex=s" => \$csv_flex, "help" => \$help ); if ($help || !$csv_add || !$csv_flex ) { die " Description: Provide the csv_add and csv_flex Usage $0 \t-csv_add <path> -csv_flex <path> : full csv file paths "; } print "$csv_add\n"; print "$csv_flex\n"; open (CSVADD_FILE,"$csv_add") or die "Could not open $csv_add in read +mode OR there is no file with name $csv_add"; open (CSVFLEX_FILE,"$csv_flex") or die "Could not open $csv_flex in re +ad mode OR there is no file with name $csv_flex"; #removing the spaces between the fields of csv open(STDOUT, "+>csv_add_up"); rm_spaces(\*CSVADD_FILE); close STDOUT; open(STDOUT, "+>csv_flex_up"); rm_spaces(\*CSVFLEX_FILE); close STDOUT; #chomp removes any new line character at the end of line and can retur +n number of elements removed #remove spaces and /I/0 and /T/0 sub rm_spaces { my $file = shift; while (<$file>) { chomp; my @fields = split /,/; s/\s+//g for @fields; s/\/I\/0//g for @fields; s/\/T\/0//g for @fields; print join ",", @fields,"\n"; } }

Please suggest on how to make a single function that does the work of rm_spaces() and takes different files.

Replies are listed 'Best First'.
Re^3: Inline change to the file passed by Glob
by poj (Abbot) on Aug 23, 2017 at 06:52 UTC
    #!/usr/bin/perl use strict; my $csv_add = 'add.csv'; my $csv_flex = 'flex.csv'; print " csv_add : $csv_add csv_flex : $csv_flex\n"; for my $filename ($csv_add,$csv_flex){ if (-e $filename){ rm_spaces($filename); } else { warn "$filename not found"; } } # remove spaces,tabs,newlines,formfeeds,cr # and /I/0 and /T/0 sub rm_spaces { my $filename = shift; my $bakfile = $filename.'.bak'; rename ($filename,$bakfile) or die "Could not rename $filename to $bakfile"; open my $fh_in, '<',$bakfile or die "Could not open $bakfile"; open my $fh_out,'>',$filename or die "Could not open $filename"; while (<$fh_in>) { s/\s+|\/[IT]\/0//g; print $fh_out "$_\n"; } close $fh_in; close $fh_out; unlink $bakfile; }
    poj
Re^3: Inline change to the file passed by Glob
by Anonymous Monk on Aug 23, 2017 at 06:40 UTC
    Read perlintro, pass two(or 11teen) filenames as arguments to the function