By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,321 Members | 1,909 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,321 IT Pros & Developers. It's quick & easy.

How to comapre the file name in perl ???

P: 65
hi all,

i am reading the content of the directory & then putting if condition to comapre the file name.

problem is: filename ends with number.
eg :prstat-Lvs-20080118-2100

the first part of the file name is constant but the later part (20080118-2100) is not constant

Expand|Select|Wrap|Line Numbers
  1. my $dir = "C:\\Performance_svap\\INPUT_FILES\\*";
  2. my @file=glob("$dir");
  3. my $f;
  4. my @prstat_Lvs;
  5.  
  6. foreach $f (@file){
  7.     if ($f =~ /prstat-Lvs/){
  8.             push (@prstat_Lvs,$f);
  9. }
  10.  
now my question is how to compare the full file name in if condition ???
do i need to give like this :
Expand|Select|Wrap|Line Numbers
  1. if($f =~/prstat-Lvs-[0..9]-[0..9]/){
  2.  
can anyone help me out ???

Regards,
Vijayarl
Sep 30 '08 #1
Share this Question
Share on Google+
6 Replies


nithinpes
Expert 100+
P: 410
If the number of digits/pattern(20080118-2100) remains same, you can use:
Expand|Select|Wrap|Line Numbers
  1. if($f =~/prstat-Lvs-[0..9]{8}-[0..9]{4}/){ 
  2.  
Inside curly brace, you will be specifying the number of occurence. You can even give a range. e.g.
\d{2,8} - minimum 2 digits and a max. of 8
Sep 30 '08 #2

P: 65
Thanks Nithinpes !!!!!!

This what exaclty i want !!!! :-)
Oct 1 '08 #3

P: 4
you can try this way also
Expand|Select|Wrap|Line Numbers
  1. if ($f =~ /^prstat-Lvs/){
  2.              push (@prstat_Lvs,$f);
  3.  }
  4.  
Oct 1 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
does this work?

Expand|Select|Wrap|Line Numbers
  1. if($f =~/prstat-Lvs-[0..9]{8}-[0..9]{4}/){ 
I would think the character classes need to be written like this:

[0-9] instead of [0..9]

that is literally a dot in a character class, not a range operator.
Oct 1 '08 #5

nithinpes
Expert 100+
P: 410
does this work?

Expand|Select|Wrap|Line Numbers
  1. if($f =~/prstat-Lvs-[0..9]{8}-[0..9]{4}/){ 
I would think the character classes need to be written like this:

[0-9] instead of [0..9]

that is literally a dot in a character class, not a range operator.
Yes, that's right Kevin.I posted it in a hurry. Range operator does not work inside character class. I wonder how it worked for the OP!
Oct 3 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
I wonder how it worked for the OP!
Not too well I suspect. ;)
Oct 3 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.