0

Still new to perl and easy ways to work around it so I am seeking help ! I am sure this issue isn't a very difficult one to solve but I am still stuck nonetheless...

I would like to see the progression of this bit of my script:

my @goods = map {   my @vals = @{$ids_for{$_}};
                    my @matches = map { my $tf_id = $_;
                                        grep {$_ =~ $tf_id} @vals } @tf_ids;
                    my $num_match = scalar(@matches);
                    ## $num_match
                    for my $match ( @matches[0..($num_match-1)] ){
                        say {$out} "./" . $_ . ".fasta:" . $match if (scalar(@matches) > 0);
                    }

        } @keys;

## @goods

The input of the script is a file containing ids looking like this:

>7_54 lcl
>7_264 lcl
>7_332 lcl
>7_427 lcl
>7_598 lcl
>7_772 lcl

There are a couple thousands of them which is why I would like to know the progress made !

Thanks for your replies ! Let me know if you need any more essential info that I might have forgotten.

Nicolas
  • 11
  • 2
  • 2
    There are a couple of different modules for progress bars on CPAN. You can run a quick search to get them. They all have in common that they need to know how far you already have progressed. So you will have to know the number of rows, or items. It looks like you know that, so that's great. – simbabque Mar 11 '20 at 14:44
  • 2
    Side-note: your `map` is really long and doesn't really make the code more readable. Consider using a `foreach` loop instead to make your life easier when you get back to reading this code later. – simbabque Mar 11 '20 at 14:45
  • Thanks for the reply ! I'll do some more research on progress bars then I must have missed some. I'll consider switching to a `foreach` loop as well ! – Nicolas Mar 11 '20 at 14:48
  • 1
    Some suggestions here (you'll probably need to come up with code to calculate the percentage complete): https://stackoverflow.com/questions/21300099/progress-bar-in-command-line-perl-script – Possum Mar 11 '20 at 20:11

1 Answers1

1

These methods was used for ages and does not require any modules or GUI and nevertheless they performed their function nicely.

NOTE: sleep delay added to simulate working progress for visual effect

use strict;
use warnings;
use feature 'say';

my @data = <DATA>;
chomp @data;        # snip eol
my $count = 0;
my $total = @data;

say "\nMethod #1";
for (@data) {
    $count++;
    say "[$count/$total] $_";
    sleep 1;
}

$| = 1;             # turn off output buffering

say "\nMethod #2";
$count = 0;
for (@data) {
    $count++;
    printf "[%d/%d] %s            \r",
            $count, $total, $_;
    sleep 1;
}

say "\nMethod #3";
$count = 0;
for (@data) {
    $count++;
    printf "[%.2f %%] %s            \r",
            100*$count/$total, $_;
    sleep 1;
}

say "\nMethod #4";
$count = 0;
for (@data) {
    $count++;
    my $percent = 100*$count/$total;
    printf "%-16s %6.2f %% %-51s 100%%\r",
            $_, $percent , '=' x int($percent/2) . '>';
    sleep 1;
}

say "\nBonus";
my @spinner = ('-','\\','|','/');
for (@data) {
    $count++;
    printf "[%d/%d] %6.2f%%  %s\r",
        $count, $total,
        100*$count/$total,
        $spinner[$count%4];
    sleep 1;
}

say "\nDone...";

__DATA__
>7_54 lcl
>7_264 lcl
>7_332 lcl
>7_427 lcl
>7_598 lcl
>7_772 lcl
>7_54 lcl
>7_264 lcl
>7_332 lcl
>7_427 lcl
>7_598 lcl
>7_772 lcl
>7_54 lcl
>7_264 lcl
>7_332 lcl
>7_427 lcl
>7_598 lcl
>7_772 lcl
>7_54 lcl
>7_264 lcl
>7_332 lcl
>7_427 lcl
>7_598 lcl
>7_772 lcl
Polar Bear
  • 6,762
  • 1
  • 5
  • 12