#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    10
    Rep Power
    0

    Matching expression inside an "if" statment


    Dear All

    I have a question that I think it very simple but I cant find the answer myself...

    I need that the following IF will return my TRUE if it will find any expression that ends with a certain extension:

    if ( -s $mynameNO_MATTER_WHAT_I_GOT_HERE".".$ext )

    its need to find some name then everything I want and then dot and specific extension.
    For example:

    dave_A_B_C.ps
    or
    dave_myName.ps

    Can you please advise?

    I tried to do something like this:
    if ( $mynameNO."*.$ext" )
    and it didnt worked either..

    Thanks in advance!!
  2. #2
  3. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,252
    Rep Power
    1810
    Is this for perl, or some other language? I don't recognize your use of the -s operator.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    828
    Rep Power
    496
    Hi,

    I also think it is probably quite simple, but I don't understand your question.

    Are you working on strings from a file? On filenames? or what?

    Assuming you are working on filenames, the glob function might be what you need.

    This is an example under the Perl debugger of what you could get:

    Perl Code:
      DB<1> @list = glob ("a*.pl");
     
      DB<2> x @list
    0  'a.pl'
    1  'add2.pl'
    2  'analyse_fable.pl'
    3  'any.pl'
    4  'aoh.pl'
    5  'array_hash.pl'
      DB<3>


    The first line is looking for all ".pl" files starting with the letter a on my default directory and stores this list into the @list array. The second line shows all the file names in the @list array.

    Is this what you needed?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    10
    Rep Power
    0
    Hi

    Thank you for the answer.
    YES this is perl.
    I didnt understood either what is the -s..

    I am working in unix so I need to find in the current folder all the files that have $name*.$ext

    and I need it in IF statement:
    for example if I have in folder 2 files:
    dave_A_B_C.ps
    dave_myName.ps

    if ($name*.$ext) {

    Do something

    }


    Thanks again!!
  8. #5
  9. !~ /m$/
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    May 2004
    Location
    Reno, NV
    Posts
    4,252
    Rep Power
    1810
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    my $dir = '.';
    my $name = '6750';
    my $ext = 'pdf';
    
    opendir(my $dh, $dir) || die "Can't open directory: $!";
    
    while(readdir $dh) {
    	if (/^$name.*\.$ext$/) {
    		print "$_\n";
    	}
    }
    closedir $dh;
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    Originally Posted by MA1985
    Hi
    I didnt understood either what is the -s..
    -s is a file test operator and returns the size of the file.

    Originally Posted by MA1985
    I am working in unix so I need to find in the current folder all the files that have $name*.$ext

    and I need it in IF statement:
    for example if I have in folder 2 files:
    dave_A_B_C.ps
    dave_myName.ps

    if ($name*.$ext) {

    Do something

    }


    Thanks again!!
    Besides using opendir/readdir or the glob function as has already been shown, you can also use the <> diamond operator.
    Code:
    while( <$name*.$ext> ) {
        print "$_\n";
    }
    Using an if statement isn't really required to find the files unless it's a requirement for a homework assignment.
    Code:
    while( <*.$ext> ) {
        if ( /^$name/ ) {
            print "$_\n";
        }
    }

IMN logo majestic logo threadwatch logo seochat tools logo