Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unless statement -- Confusing..

Status
Not open for further replies.

Milleniumlegend

IS-IT--Management
Dec 16, 2003
135
This issue is in addition to the thread thread219-1076991.

I am not able to find out what is happening here. Could someone please shed some light on this.

I have three types of files as shown as discussed in previous issue.

This is the first statement in the unless loop. I am assuming this matches the pattern where the file is of type
XXX-_TROFIES---_20051105_1104-105027-LIPS-001_USERM-----_001.RPT.

Code:
Type=1
unless ($basename =~ /^(.{4})_(.{10})_(.{8})_(.{6})_(.{10}) _(.{8})\.(.{3})\.gz$/)

There is another variation here for the same file.
XXX-_NUMBERS---_20050305_153604_ISSUED_XXX_00003109.csv
Code:
unless($basename =~ /^(.{4})_(.{10})_(.{8})_(.{20})_(.{10})_(.{3})\.(.{3})\.gz$/)

Also there is another file type
XXX-_DISP-HEPL-_20050428_DISBURSEMENT422-----_USERV-----_030.RPT.gz
Code:
unless($basename =~ /^(.{4})_(.{10})_(.{8})_(.{20})_(.{10})_(.{3})\.(.{3})/)


I need to find out how the logic fits in for the above three file types. Also I wanted to know if the unless statement does check the statement and continues if it does not match.

Many thanks



Many thanks
 
unless is the negative of if, and is designed to be used to make code clearer. It means 'if not(condition) then...'. example - you want to print a file but exclude any blank lines:
Code:
while (<FILE>) {
   if (!/^\s*$/) {
      print $_;
   }
}

# is the same as

while (<FILE>) {
   unless (/^\s*$/) {
      print $_;
   }
}

# or the idiomatic perl way

while (<FILE>) {
   print $_ unless (/^\s*$/);
}
The standard way of coding 'if not condition then print' makes the intention less clear than the perl idiom 'print all the lines unless some condition is true'. OK, that's what unless is supposed to be used for. In the case of the code you've posted, it's more of an exercise in obfuscation.

The pattern matching in the regular expressions matches on the positions of the underscores, overall length of the name, and the suffix. So, the TROFIES file name doesn't match the first expression. The underscores are in the wrong place, and it doesn't end in .gz. So the unless is executed, trying the second regex. The underscores are in the right places, but it's not a .gz file, so it doesn't match. Third regex: underscores in the right place - it matches. The unless condition is not met.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top