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

Hi Everyone, I have a file something like this:
chr10 180833 225933 1 1 0 1 0 16 chr10 181144 225933 1 1 0 2 0 13 chr10 181243 225933 1 1 0 4 0 32 chr10 181500 225933 1 1 0 5 0 34 chr10 196199 196457 1 1 0 1 0 31 chr10 226069 243850 1 1 0 1 0 25 chr10 226069 255828 1 1 0 32 0 37 chr10 255989 267134 1 1 0 24 0 33 chr10 255989 282777 1 1 0 13 0 38 chr10 267297 282777 1 1 0 33 0 38
I want to write a script in perl where I can print out those lines in which column 7 is greater than 10. my code is as follows:
while(<>){ chomp; my @s = split /\t/,$_; my $count = $s[6]; if($count > 10){ print $_,"\n"; } }

What i want to do is use Getopt::Long module and make a flag --unique so that I can feed any value to that flag let's say 2 or 4 or 10 etc and it prints the line fulfilling the condition. How can I do that?

Thanks Varun

Replies are listed 'Best First'.
Re: using Getopt::Long
by Corion (Patriarch) on Sep 27, 2016 at 19:43 UTC

    What part of modifying your script do you have the problem with? Is it with adding the functionality of the unique feature? This would be solvable by reading perlfaq4 on "unique". Or is your problem with the usage of GetOpt::Long? Then it's as easy as the following:

    GetOptions( 'u|unique' => \my $unique, );
      How would the full code look like?
      #!/usr/bin/perl-w use strict; use warnings; use Data::Dumper; use Getopt::Long; my $unique; GetOptions( 'u|unique' => \my $unique, ); while(<>){ chomp; my @s = split /\t/,$_; my $count = $s[6]; if($count > 10){ print $_,"\n"; } }
      $unique somehow should replace $count in the loop right??

        It helps us help you better if you are more explicit about where exactly you encounter problems.

        What part of perlfaq4 is problematic for you? I think especially the section on How can I remove duplicate elements from a list or array applies to your problem.

        Maybe you can adapt the code and approach presented there and get back to us when you encounter concrete problems. Please take the time to describe the problem you encounter, together with the input you give, the output you get and the output you expect.

        Did you try it? Would trying it take longer than typing a reply to ask a question, then waiting for answers, then trying it anyway? You're using Perl right? You are supposed to be lazy. Lazy is doing the least work. In this case trying it yourself is the least work by far.

        Premature optimization is the root of all job security
Re: using Getopt::Long
by GotToBTru (Prior) on Sep 27, 2016 at 19:46 UTC

    This is something covered by the documentation.

    But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)