Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Numerical Value question

by OGProphet (Initiate)
on May 27, 2016 at 02:59 UTC ( [id://1164249]=perlquestion: print w/replies, xml ) Need Help??

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

Hey guys!

So I'm very new at this so please bear with me.

I've created a script that allows you to print specific excel files within a folder based on the keyword. The keyword is consistent with all the files e.g., 160526GRAD.

Everything works fine except for this specific part of the file name where the number changes all the time. ex 160526GRAD-T5, 160526GRAD-T6, 160526GRAD-T7.

My question is, is there a way to have the script look for any number value or a range of values (0-100) when trying to look for that file? I've searched hours but I haven't been able to find what I need =(

my $a = qw(0,100); $keyword_EVO100 = "$path"."Output\\$keyword_EVO100"; $keyword_folder = "$path"."Output\\$keyword\_$initial\_$i";
my $PRIMERMAP_open = $Excel->Workbooks->Open("$keyword_folder\\$keywor +d\_EVO100\\$keyword\-T$a\_PRIMERS_PrimerList.xlsx"); my $PRIMERMAP = "$keyword_folder\\$keyword\_EVO100\\$keyword\-T$a\_PRI +MERS_PrimerList.xlsx"; if(!(-e $PRIMERMAP)) { print "Primer List file is missing. Please make sure file is in correc +t folder!\n"; print "Press <Enter> to exit\n"; <>; exit(1); } $PRIMERMAP_open->PrintOut; $PRIMERMAP_open->close;

Replies are listed 'Best First'.
Re: Numerical Value question
by davido (Cardinal) on May 27, 2016 at 03:35 UTC

    First look at this line:

    my $a = qw(0,100); print $a, "\n"; # prints the string '0,100'

    You probably mean my @a = (0,100);, or  my @a = qw(0 100). The point is that $a is a scalar, and @a is an array. And qw(0,100) is a single item, whereas qw(0 100) is a list of two items.

    Next, only open if the file exists... and use a loop.

    my $keyword_EVO100 = "${path}Output/$keyword_EVO100"; my $keyword_folder = "${path}Output/${keyword}_${initial}_$i"; foreach my $suffix (0..100) { my $PRIMERMAP = "$keyword_folder/${$keyword}_EVO100/$keyword-T${su +ffix}_PRIMERS_Primerlist.xlsx"; if (! -e $PRIMERMAP) { print "Primer list file is missing. Please make sure file is i +n correct folder!\n"; print "Press <ENTER> to exit.\n"; <>; exit(1); } $my $PRIMERMAP_OPEN = $Excel->Workbooks->Open("$keyword_folder/${k +eyword}_EVO100/$keyword-T${suffix}_PRIMERS_PrimerList.xlsx"); $PRIMERMAP_open->PrintOut; $PRIMERMAP_open->close; }

    Also notice how you can use "${varname}other_text", and also that when dealing with file paths in Perl you can almost always substitute \\ with /, even on Windows.

    Next ask yourself if you really enjoy hitting caps lock all the time to type your variables. why all the upper cases? Why such long names? (We're all guilty, just making an observation).


    Dave

Re: Numerical Value question
by AnomalousMonk (Archbishop) on May 27, 2016 at 04:12 UTC
    my $a = qw(0,100);

    Further to davido's comments above about this very suspicious statement: You don't seem to be using warnings, otherwise you'd get one about this semantic usage:

    c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; my $s = qw(0,100); print qq{'$s'}; " Possible attempt to separate words with commas at -e line 1. '0,100'
    It's always wise to enable warnings (and strict syntax restrictions as well), especially if you are a novice Perler (and in this case, see also diagnostics).


    Give a man a fish:  <%-{-{-{-<

Re: Numerical Value question
by vinoth.ree (Monsignor) on May 27, 2016 at 03:50 UTC
    Hi,

    Even you can use readdir() to each filename in folder. This code reads your folder and match for the word 160526GRAD-T with min 1 and max 3 digits after 'T'

    #!/usr/bin/perl -w opendir(DIR, "/your folder path"); @files = grep(/160526GRAD-T\d{1,3}$/,readdir(DIR)); + closedir(DIR); foreach $file (@files) { print "$file\n"; }

    All is well. I learn by answering your questions...
Re: Numerical Value question
by 1nickt (Canon) on May 27, 2016 at 03:24 UTC

    You might like to use File::Find::Rule:

    my @files = File::Find::Rule->file() ->name( qr/your regexp here/ ) ->in( $dir );

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Numerical Value question
by Laurent_R (Canon) on May 27, 2016 at 06:19 UTC
    The easiest to retrieve all your files is probably to use the glob function. It will list the files you are looking for just as ls or dir would do at the OS prompt, depending on whether you are on Linux or Windows. For example with something like this:
    my @files = glob ("$source_dir/160526GRAD-T*");

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1164249]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-18 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found