Multi Commander > Script

Title Case

<< < (3/3)

Mathias (Author):

--- Quote from: Ulfhednar on July 07, 2018, 17:51:57 ---OK I'm incorrectly thinking that because

--- Code: ---for( $n = 0; $n < $items; $n++ )
--- End code ---
works for multiple files - it will call the same thing ($arr) in 2 different ways --- which it won't  :o

I need to differentiate...  Thank you Mathias, I will try again & stop destroying your weekend now !   :D

--- End quote ---

Yhaa.  But then $n is used someway..  What is $n ?  and what is  $arr
Sorry,. I don't understand your reasoning how that should work, it make no logical sense :)

$arr is an array (list) of text strings.  there are many text strings in that list.. and when you convert one of the text in the list you need to grab the item you want from the list.
For example "@var $NameA = PathGetNamePart( $arr )"  you send a list of many text string (that are paths) to a function that return a new text. not a list of text.. a single text.
So, there is no logic in sending an entire list of items to that function.. you need to send the specific item you want from the list into the function



Ulfhednar:

--- Quote from: Mathias (Author) on July 07, 2018, 18:19:07 ---
Sorry,. I don't understand your reasoning how that should work, it make no logical sense :)

--- End quote ---
I know!  :D

Thanks to your input, I realized I hadn't assigned iterations & needed more references for the arrays & got it to work - I am still finding it hard to balance how smart code can appear with how dumb the machine really is  :P


--- Code: ---@var $exceptionsArray[] = { "a", "as", "an", "and", "any", "at", "for", "from", "into", "is", "of", "on", "or", "the", "to", };
@var $n;
@var $oldN;

function TitleCase($name)
{
   return  _TitleCase($name, " ");
}

function _TitleCase($name , $splitchar)
{

  @var $result = "";
  @var $parts = StrSplit($name , $splitchar); // space and - will split string
 
  @var $cnt = arrayCount( $parts );
  for( $n = 0; $n < $cnt ; $n++ )
  {
    @var $textPart = $parts[ $n ];
    // To Lower
    $textPart = StrToLower( $textPart );
 
    if( StrFind( $textPart , "-", 0 ) > 0)
    {
      // Becuse of recursive bug, $n is is overwriten
      $oldN = $n;
      $textPart = _TitleCase($textPart , "-");
      $n = $oldN;
    }

    if( arrayFind( $exceptionsArray, $textPart ) == -1)
    {
    // Uppser case on first
    $textPart[0] = StrToUpper( $textPart[0] );
    }

    if( StrLen( $result ) > 0 )
$result += $splitchar;

    $result += $textPart;
  }
  return $result;
}
 

@var $arr1 = GetSourceSelectedPaths();
@var $items = arrayCount($arr1);
@var $CurrentNameFullPath;
@var $OrgName;
@var $NewName;
@var $n1;


for( $n1 = 0; $n1 < $items; $n1++ )
{
 
@var $arr = GetSourceSelectedPaths();
@var $NameA = PathGetNamePart( $arr[$n1] );
@var $newText = TitleCase($NameA);



  $CurrentNameFullPath = $arr1[ $n1 ];
  $OrgName = PathGetNamePart( $CurrentNameFullPath );
  $NewName = StrReplace( $OrgName, $NameA, $newText );
 
RenameFile( $CurrentNameFullPath, $NewName, "RENAME_RO" );
}

--- End code ---

I obviously haven't made it as clean as it might be, but it is a start. 
Think I have abused the { } .  ::)

I am now wondering about capitalizing the first word where the first word is in the exceptions list e.g.
For MultiScript coding disasters this is the place
becomes
for Multiscript Coding Disasters This is the Place

I may need to plagiarize some of that function you created...  ;)

UPDATE
Added this

--- Code: ---@var $char1 = StrSub($NewName, 0, 1);
@var $char2 = StrToUpper( $char1 );
@var $NameB = StrReplace( $NewName, $char1, $char2 );
 
RenameFile( $CurrentNameFullPath, $NameB, "RENAME_RO" );
--- End code ---

so now the emergency is over, I can go to sleep :D :P



........I also wondered about the recursive bug $n/$oldN is that something that only manifests in this type of function?

Mathias (Author):
If you look at my code.. it will make everything lowercase first.. then when it handled everyword it uppcase the first letter with


--- Code: ---$textPart[0] = StrToUpper( $textPart[0] );
--- End code ---
This will take the first character if textPart and insert it into the first postion in textPart

You can access individual characters using the [.], think of the string and an array (list) of characters

$textPart[0] = $textPart[1]; 
This will take the seconds characters and overwrite the first character with that. so the first two characters are now the same


--- Quote from: Ulfhednar on July 08, 2018, 14:59:16 ---........I also wondered about the recursive bug $n/$oldN is that something that only manifests in this type of function?

--- End quote ---

It is when you have a functions that is calling it self. The problem is that all the local variables are not unique then. so it overwrite when it should not.
That trick $n to the old state it had before the function was called because when it called it self it changed the value of that variable.

Ulfhednar:
Thanks Mathias.  I appreciate the help. :)

My solution was more verbose.  My primary focus was to get a working version.  Now I can use this as a reference whilst I am playing about trying to tweak & tidy my code.
I had looked at the

--- Code: ---$textPart[0] = StrToUpper( $textPart[0] );
--- End code ---
entry & was going to try it, but decided that I would probably create a conflict/problem.    ::)
So I went with what I knew would work with this task as a logical progression/last check on the first word case status. :P
I'd also wondered about adding a line equating to something like "ignore exceptions array if word starts with/at character 0" within your function.

I intend to come back to your function code as I need to understand how you have used scope & therefore how to use that $textPart[0] to check first word capitalization as the last operation before generating $NewText..

I think I understand about $n, that is interesting & raises more questions for me. I'm glad you have shown me that this can happen or I am sure I would have been driven crazy had I accidentally discovered it alone :)   

Ulfhednar:
I managed to plagiarize that piece  of code Mathias, I hadn't been sure how/where to insert it but I figured it out :P


--- Code: ---@var $arr = GetSourceSelectedPaths();
@var $NameA = PathGetNamePart( $arr );
@var $newText = TitleCase($NameA);
@var $NameB = $newText[0] = StrToUpper( $newText[0] );

{
RenameFile( $arr, $newText, "RENAME_RO" );
}
--- End code ---

Thanks again. ;)

BTW do you use any specific syntax coloring to help when when coding with MS?  I find it helps me if I can use colors when I'm trying something.  ATM I am using notepad++.  Any recommendations for coloring?   (It's probably a big job but maybe you can add syntax coloring to the MS Debugger some time....?)

Navigation

[0] Message Index

[*] Previous page

Go to full version