in reply to Why this script is not running on windows?

Hi abhikalrt53,

Why not turn it into a real script?

$ perl -MO=Deparse -ane '$s = ""; foreach (@F) { /(\d+)-(\d+)/ and $2 +- $1 >= 30 and $s .= " $1-$2" } print "$F[0]$s\n" if $s' LINE: while (defined($_ = <ARGV>)) { our @F = split(' ', $_, 0); $s = ''; foreach $_ (@F) { $s .= " $1-$2" if /(\d+)-(\d+)/ and $2 - $1 >= 30; } print "$F[0]$s\n" if $s; }

With a bit of cleanup:

#!/usr/bin/env perl use warnings; use strict; while (<>) { my @fields = split; my $s = ''; for (@fields) { if ( /(\d+)-(\d+)/ && $2 - $1 >= 30 ) { $s .= " $1-$2"; } } print "$fields[0]$s\n" if $s; }

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^2: Why this script is not running on windows?
by abhikalrt53 (Novice) on Nov 01, 2016 at 11:01 UTC

    Hey Hauke how are you?

    This code also seems fine.. I want to ask you another question as I could not find a way to do it in my one liner.

    This Script takes on a input file like:

    NP_416485.4: 970-970, 991-992, 1126-1235 NP_417679.2: 209-413, 418-421, 441-542

    and gives a output like:

    NP_416485.4: 1126-1235 NP_417679.2: 209-413 441-542

    But instead of only <space> in output, I want a output like the input format.. i.e.,comma then space:

    NP_416485.4: 1126-1235 NP_417679.2: 209-413, 441-542

    Thanks

      Hi abhikalrt53,

      One way to do this would be to store the ranges in an array, and then use join when outputting the array.

      A simpler way to do it would be to add the comma only under certain conditions, i.e. $s .= ',' if ...; - I'm not going to give away the condition because I don't think it's appropriate I do all of your (home)work for you ;-)

      Hope this helps,
      -- Hauke D

        thank u sir