Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How to Pass more than one file in perl MY function

by prad001 (Initiate)
on Sep 29, 2022 at 15:04 UTC ( #11147149=perlquestion: print w/replies, xml ) Need Help??

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

Hi Team,

I am new to Perl and was wondering if you guys can help me in regards to passing more than one files in the below code;

my @files=<data/j*.*.txt>; if (@ARGV) { my $test=$ARGV[0]; $test=lc($test); print "Using $test instead\n"; @files=</data/$test*.*.txt>; print "Found @files instead\n"; } my $outfile='/data/w_c.txt'; my $lotfile='/data/completed.txt'; if (-e $outfile) { unlink $outfile; }
In the above code (my @files=<data/j*.*.txt>;) is currently having all the files starting with j*.*, But I would like to pass all the below files only;
j*.1.txt
j*.3.txt
j*.a.txt
etc..

How could I pass the list of files?

Thank you,
Pradeep

Replies are listed 'Best First'.
Re: How to Pass more than one file in perl MY function
by hippo (Bishop) on Sep 29, 2022 at 15:39 UTC
    But I would like to pass all the below files only; j*.1.txt j*.3.txt j*.a.txt etc..

    These examples all have only 1 character between the dots. If you just want that then you can amend the glob to look for that:

    my @files = <data/j*.?.txt>;

    OTOH, if you want to pass arbitrary globbing patterns you could do that in @ARGV but you'll need to quote them on invocation.

    #!/usr/bin/env perl use strict; use warnings; my @files = <data/j*.*.txt>; if (@ARGV) { @files = map { <data/$_> } @ARGV; } print "Using @files";
    $ ./gt.pl Found data/j.a.txt data/j.foo.txt data/j.xyz.txt $ ./gt.pl 'j*.?.txt' Using j*.?.txt instead Found data/j.a.txt

    See map for more on its usage.


    🦛

      What if the file names are different.. something like /data/j*.1.txt /data/j*.txt /data/q*.1.txt etc..!! Please let me know.. Thank you in advance..

        Well then you would pass them as separate args:

        $ ./gt.pl 'j*.1.txt' 'j*.txt' 'q*.1.txt'

        Obviously the first argument here is redundant as it is a subset of the second.


        🦛

Re: How to Pass more than one file in perl MY function
by tybalt89 (Monsignor) on Sep 29, 2022 at 19:22 UTC
      Nice. I didn't expect that to work, but it does. I couldn't find any spec on what is allowed in the glob syntax, all I knew for sure was * and ?. I didn't know about a character set. Learned something new - thanks.
Re: How to Pass more than one file in perl MY function
by GrandFather (Saint) on Sep 29, 2022 at 22:31 UTC

    You may find File::Find helps clean up the logic:

    use strict; use warnings; use File::Find; my @matches = (qr(.*\.pl$), qr(.*\.pm$)); find(sub{wanted ($_, @matches)}, '.'); sub wanted { my ($name, @m) = @_; print "$File::Find::name\n" if grep {$name =~ /$_/} @m; }

    Prints (on my system - your results will be different):

    ./noname1.pl ./noname1.pm ./noname2.pl ./noname2.pm ./SortaIniParser.pm ./StarTrek.pl ./Win32/FileVersionInfo.pm ./Win32/PEFile.pm

    You can replace the grep {$name =~ /$_/} @m test with whatever suits your purpose.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      Thank you for the reply. How to pass location of the files in the above code? For example: I have all the files in /data/test/ location. Also, I have almost 30 unique file formats/types to search. In your example you have specified only 2, is there a limit of how many file formats I can pass?
Re: How to Pass more than one file in perl MY function
by choroba (Cardinal) on Sep 30, 2022 at 13:57 UTC
    Crossposted to StackOverflow.

    It's considered polite to inform about crossposting, so that people not attending both sites don't waste their efforts hacking a problem that has already been solved at the other end of the internets.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: How to Pass more than one file in perl MY function
by BillKSmith (Monsignor) on Sep 29, 2022 at 19:06 UTC
    If you prefer the syntax of regex to that of glob, you can glob the list as you do now and then clean it up with the perl function glob.
    Bill
      clean it up with the perl function glob.

      no

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2023-03-31 18:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (76 votes). Check out past polls.

    Notices?