http://qs321.pair.com?node_id=1197407

gowthamvels has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: if condition within module
by wjw (Priest) on Aug 15, 2017 at 15:11 UTC
    Perhaps your question is a bit unclear because of the terminology you are using. I am assuming that you are pretty new to Perl(and perhaps programming in general?).

    I am not familiar with Data::Table, so will offer my suggestion outside the scope of what it might be capable of. You can use an if statement pretty much anywhere in your code that you wish: In a sub, in a module, it does not matter. Based on what you state that you want, a code snippet that would work might look like the following:

    if ($col6 =~ /^\Q97517\E/ ) { --do somethng -- } elsif ($col6 =~ /^\Q97516\E/) { --do something else -- }

    The --do something(s) -- can be what whatever you want them to be.

    If you are up against a time constraint, such as an assignment due, it will be much faster to look in the documentation for Data::Table. There is an example about half way down that shows the use of an if() conditional.

    There is no better way to learn Perl and programming in general than to take a problem one wants to solve and figure out how to solve that problem using the language. But you have to take the time to learn the terminology so that you can ask questions which are clear when seeking help. Learning takes time, and time constraints are not conducive to learning programming in my experience....

    ...the majority is always wrong, and always the last to know about it...

    A solution is nothing more than a clearly stated problem...

Re: if condition within module
by hippo (Bishop) on Aug 15, 2017 at 13:03 UTC
Re: if condition within module
by marinersk (Priest) on Aug 15, 2017 at 10:58 UTC

    You are asking how to use if()in a module.

    1. Your code doesn't show a module
    2. The use of if()isn't any different in a module than a main script.

    So I'm not sure how to answer your question.

    Is it possible you are really asking how to write a module?

      Thanks for the response, I am asking for how to use the if() in this code

        I'm unfamiliar with Data::Table, a quick scan of the documentation shows examples of the various matching methods, match_pattern,match_string and so on, which you could use. The documentation also links to a PDF cookbook. I suggest you learn to use the tools you've chose, on in this case, that you've decoded to used based upon someone else giving you this code.

Re: if condition within module
by kcott (Archbishop) on Aug 16, 2017 at 07:24 UTC

    G'day gowthamvels,

    I want to use a if conndtion like below ...

    if ($col6 =~ /^\Q97517\E/ } elsif $col6 =~ /^\Q97516\E/))

    The very first thing you should do is read "Perl introduction for beginners" because, from what you've demonstrated, you do not, at present, have a basic understanding of Perl code syntax.

    if can be used in a compound statement. The construct looks like this:

    if (CONDITION) { ACTION_WHEN_CONDITION_IS_TRUE }

    That may be followed by zero or more alternative conditions and actions:

    elsif (ALTERNATIVE_CONDITION) { ACTION_WHEN_ALTERNATIVE_CONDITION_IS_TRUE }

    Finally, all of that may be optionally terminated with:

    else { ACTION_WHEN_CONDITION_IS_FALSE }

    This is documented in "perlsyn: Compound Statements".

    if may also be used as a statement modifier:

    ACTION_WHEN_CONDITION_IS_TRUE if CONDITION;

    This is documented in "perlsyn: Statement Modifiers".

    There is also an "if pragma". This does not appear to have anything to do with what you're currently attempting; however, be aware of it to avoid confusion in other (unrelated) code, discussions or documentation.

    Perl has no builtin if() function. I mention this because "if()" has been used in a number posts in this thread (including by yourself: "I am asking for how to use the if() in this code") and that usage could be confusing to a Perl novice.

    — Ken