Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what it does $line =~ s/\"//g; ?

Status
Not open for further replies.

TTMTech

Programmer
Feb 5, 2003
29
0
0
US
Hi! All

I am new to perl.I need to write modify report program for management. Below is a program file.

#!/usr/bin/perl

use strict;
use Win32::FileOp qw(OpenDialog SaveAsDialog);

my $Customers;

my $credit_list = OpenDialog( title => "Select Credit List", filters => [ 'Text File (*.txt)' => "*.txt" ]);
my $credit_limit = OpenDialog( title => "Select Credit Limit", filters => [ 'Text File (*.txt)' => "*.txt" ]);

if (!$credit_list || !$credit_limit) {
print "Error: Required input reports were not supplied.\n";
sleep 3;
exit;
}

open (IN, $credit_list);

while (<IN>) {
my $line = $_;

$line =~ s/\r|\n//g;
$line =~ s/\$//g;
$line =~ s/\&quot;//g;
$line =~ s/,//g;

my ($custname, $natl_acct, $credit_limit) = split(/\t/, $line);

$Customers->{$custname}->{natl_acct} = $natl_acct;
$Customers->{$custname}->{credit_limit} = $credit_limit;
$Customers->{$custname}->{'80percent'} = $credit_limit * .8;
$Customers->{$custname}->{over60} = 0;
}

open (IN, $credit_limit);

while (<IN>) {
my $line = $_;
$line =~ s/\r|\n//g;

$line =~ s/\$//g;
$line =~ s/\&quot;//g;
$line =~ s/,//g;
my ($natl_acct, $custname, $docnum, $total, $credit_limit, $doctype, $over60, $over90) = split(/\t/, $line);

$Customers->{$custname}->{natl_acct} = $natl_acct;
$Customers->{$custname}->{over60} = $over60;
$Customers->{$custname}->{over90} = $over90;

$Customers->{$custname}->{total} += $total;
}

close (IN);

# SA wip data
open (IN, '\\\\ftpserver\schedule\wip\today\sablog.txt') || die &quot;Couldn't get SA WIP data: $!\n&quot;;

while (<IN>) {
my $line = $_;

$line =~ s/\r|\n//g;

$line =~ s/\$//g;
$line =~ s/\&quot;//g;
$line =~ s/,//g;

$line =~ s/^\s+//g;
$line =~ s/\s+$//g;

$line =~ s/\^\s+/^/g;
$line =~ s/\s+\^/^/g;

my (undef, $custname, undef, $total) = split(/\^/, $line);
next if (!$custname);

$Customers->{$custname}->{total} += $total;
$Customers->{$custname}->{wip} = &quot;WIP&quot;;
}

close (IN);

# RD wip data
open (IN, '\\\\ftpserver\schedule\wip\today\rdblog.txt') || die &quot;Couldn't get RD WIP data: $!\n&quot;;

while (<IN>) {
my $line = $_;

$line =~ s/\r|\n//g;

$line =~ s/\$//g;
$line =~ s/\&quot;//g;
$line =~ s/,//g;

$line =~ s/^\s+//g;
$line =~ s/\s+$//g;

$line =~ s/\^\s+/^/g;
$line =~ s/\s+\^/^/g;

my (undef, $custname, undef, $total) = split(/\^/, $line);
next if (!$custname);

$Customers->{$custname}->{total} += $total;
$Customers->{$custname}->{wip} = &quot;WIP&quot;;
}

close (IN);

my $final_report = SaveAsDialog( title => &quot;Save As&quot;, filters => [ 'CSV (Comma delimited) (*.csv)' => &quot;*.csv&quot; ]);

if (!$final_report) {
print &quot;Error: Output file was not specified.\n&quot;;
sleep 3;
exit;
}

open (OUT, &quot;>$final_report&quot;);

my ($day, $month, $year) = (localtime)[3,4,5];
$month++; $year += 1900;

print OUT &quot;,$month/$day/$year,,Credit Limit Report,,,,\n\n&quot;;
print OUT &quot;National Acct,Customer #,Trans. Amt,>60 Days,80\%C.L.,Credit Limit,\$-Over80%,\$-Over100%\n&quot;;
foreach my $custname (sort { $Customers->{$b}->{over60} <=> $Customers->{$a}->{over60} } keys %{$Customers}) {
my $Cust = $Customers->{$custname};
next if ($Cust->{total} <= $Cust->{'80percent'});
next if ($Cust->{credit_limit} == 0);

# Since we're sorting on over 60, if we've reached a zero then we're done since we don't show zeros.
last if ($Cust->{over60} == 0);
$Cust->{sover80} = $Cust->{total} - $Cust->{&quot;80percent&quot;};
$Cust->{sover100} = $Cust->{total} - $Cust->{credit_limit};

$Cust->{sover80} = 0 if ($Cust->{sover80} < 0);
$Cust->{sover100} = 0 if ($Cust->{sover100} < 0);

print OUT &quot;$Cust->{natl_acct},$custname,\$$Cust->{total},\$$Cust->{over60},\$$Cust->{'80percent'},\$$Cust->{credit_limit},\$$Cust->{sover80},\$$Cust->{sover100},$Cust->{wip}\n&quot;;
}

close (OUT);


I do't understand below part what it means and does :

$line =~ s/\r|\n//g;
$line =~ s/\$//g;
$line =~ s/\&quot;//g;
$line =~ s/,//g;

Is there anyone who can help me ? where can i find more meaningful information ?

Thanks in advance.

TTMTECH

 
that part is removing all the CR, NL, $, &quot; and comma characters from the string contained in $line.

Recommend Programming Perl, Third Edition, Chapter 5, Pattern Matching

Tom Morrison
 
The s///g is the substitution operator. To answer your question though;

$line =~ s/\r|\n//g; # remove all occurances of carriage returns and newlines
$line =~ s/\$//g; # remove all occurances of the '$' symbol
$line =~ s/\&quot;//g; # remove all occurances of the '&quot;' symbol
$line =~ s/,//g; # remove all occurances of the ',' symbol

In the code they could be more easily written as:

$line =~ s/[\r\n\$\&quot;,]+//g;

This wraps them all up into a character class, and deletes multiples in one go, which will be quickier than the individual substitutions. The /g modifier ensures that the pattern match is repeatedly match throughout the string.

For more information on the substitution try 'perldoc -f s' [1][2], the perlfaq6 [3] or the 'perldoc perlre' [4] document.

[1] [2] [3] [4]
Barbie
Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top