Hi Monks,
This post is regardig how do i use the if condtion statement within perl module. below is the data, the code attached is given by one of the monk, he helped me out.
I want to use a if conndtion like below if thee col6 is starting with 97517, it should pick in if condtion. in elsif condition it should pick 97516 containing records
if ($col6 =~ /^\Q97517\E/ }
elsif
$col6 =~ /^\Q97516\E/))
Input csv
col1,col2,col3,col4,col5,col6,col7,col8
1234,GP,20170715,0,V,97517,24,0.6
5678,Pack,20170715,0,V,97516,88,1.8
1234,GP,20170715,0,V,97517,22,0.6
5678,Pack,20170715,0,V,97517,183,3.9
1234,PRS,20170715,0,S,97517,261,5.4
5678,PRS,20170715,0,M,97517,36,0.9
code
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Table;
# Load input data from csv file
my $dt = Data::Table::fromCSV('data.csv');
# Make a new table that only contains the relevant columns
my $st = $dt->subTable(undef, [ 'col2', 'col7', 'col8' ]);
# Group by 'col2', calculate sums for 'col7' and 'col8'
my $ot = $st->group(
['col2'], # column to group by
['col7', 'col8'], # Columns to perform calculation on
[ \&sum, \&sum ], # Apply sum function to 'col7' and 'col8'
['sum_of_col7', 'sum_of_col8'] # Put the sums in these columns
);
print $ot->csv, "\n";
sub sum {
my @data = @_;
my $sum = 0;
foreach my $x (@data) {
next unless $x;
$sum += $x;
}
return $sum;
}
2017-08-16 Athanasius added code tags
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.