INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Parsing an "if" expression  (Read 563 times)

john greenwood

  • World Citizen
  • ***
  • Posts: 108
Parsing an "if" expression
« on: December 26, 2023, 02:55:06 pm »

I am trying to set up an SDD music drive to be used with a player that can only search files. This is not all that difficult as my file structure mimics my normal search strategy.

But I do have one problem. I need to begin each file name with the track number for them to play in the same sequence as on the album. And if the track number is less than 10, I need to insert a '0' before the track number. I've been playing with the following:

=If([Track #] < 10, 0[Track #] [Name],[Track #] [Name])

The problem is all of my tracks - including those numbered 10 and above get the '0' added - i.e. the test expression always comes back true. That's not what I want. 

I wondered whether it had something to do with numbers being treated as text characters, but I couldn't figure out how to overcome that. For instance I tried replacing 0 with Number(0), but it didn't seem to help.
Logged

terrym@tassie

  • MC Beta Team
  • Galactic Citizen
  • *****
  • Posts: 474
Re: Parsing an "if" expression
« Reply #1 on: December 26, 2023, 04:06:42 pm »

Try
Code: [Select]
PadNumber([Track #],2)-[Name]
Logged
" I like work: it fascinates me. I can sit and look at it for hours." -Jerome K. Jerome

john greenwood

  • World Citizen
  • ***
  • Posts: 108
Re: Parsing an "if" expression
« Reply #2 on: December 26, 2023, 04:22:54 pm »

Thanks!

Oddly enough, so does: =If([Track #] > 9, [Track #] [Name], 0[Track #] [Name]) - i.e. flipping the expression around.  If there's an error in the former version, I can't spot it.

In any event yours is much simpler.

Logged

terrym@tassie

  • MC Beta Team
  • Galactic Citizen
  • *****
  • Posts: 474
Re: Parsing an "if" expression
« Reply #3 on: December 26, 2023, 04:30:57 pm »

If using the 'Rename, Move & Copy files' tool then just using
Code: [Select]
[Track #]-[Name] in the Filename box gives (for example)
'Come Together.flac' is renamed as '01-Come Together.flac'
Logged
" I like work: it fascinates me. I can sit and look at it for hours." -Jerome K. Jerome

john greenwood

  • World Citizen
  • ***
  • Posts: 108
Re: Parsing an "if" expression
« Reply #4 on: December 26, 2023, 05:18:29 pm »

Alas - already copied.  But after some testing to make sure there aren't surprises, I can do the PadNumber() in bulk.
Logged

john greenwood

  • World Citizen
  • ***
  • Posts: 108
Re: Parsing an "if" expression
« Reply #5 on: December 27, 2023, 07:57:43 am »

The suggestion by @terrym@tassie works beautifully. But it leaves me with a puzzle.

Why does

=If([Track #] > 9, [Track #] [Name], 0[Track #] [Name])

work i.e. add the 0 padding when appropriate,

while

=If([Track #] < 10, 0[Track #] [Name],[Track #] [Name])

adds a 0 every time (no matter the track number)?

Logged

lepa

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 1971
Re: Parsing an "if" expression
« Reply #6 on: December 27, 2023, 08:07:10 am »

Because MC doesn't understand your syntax

both
if([Track #] > 9
if([Track #] < 10
are interpreted as different than zero (true). to do calculation you would need to use e.g. compare function

if(compare([Track #],>,9),no padding, padding)
if(compare([Track #],<,10),padding, no padding)
Logged

john greenwood

  • World Citizen
  • ***
  • Posts: 108
Re: Parsing an "if" expression
« Reply #7 on: December 27, 2023, 09:06:42 am »

I see what you're saying. Compare() would seem to do the trick.

Although

=If([Track #] > 9, [Track #] [Name], 0[Track #] [Name])

does work.




Logged

dtc

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 3019
Re: Parsing an "if" expression
« Reply #8 on: December 27, 2023, 01:06:21 pm »

MC expression syntax is slightly different than other programs, like Excel and many programming languages, where your expression would be fine.

In MC, "An expression is any sequence of literal text and any number of function calls" (Wiki). So, an expression does not understand numerical values except within a function, only literal text and expressions.   [Track #] < 10 sees 10 as a literal, not an integer. The Compare function is used to compare 2 numerical values and is needed inn an expression to do the type of comparison you want.  As I said, somewhat different but once you get caught once you remember it.

Logged
Pages: [1]   Go Up