Copy link to clipboard
Copied
I have this text:
5. GEOFF SAUNDERS
10. CAL BRUTON
11. CRAIG HERBERT
14. BRAD MILEY
8. JAMES CRAWFORD
And I want to be able to display it with full names, just last names, or first initials plus last names (numbers always present). So I came up with this code:
//set which version of first names to display
if (thisComp.layer("Null 18").effect("First Names")("Checkbox") == 1) { first = "full"; }
else if (thisComp.layer("Null 18").effect("First Initials")("Checkbox") == 1) { first = "initial"; }
else {first = "none"; }
//get full player list
full = comp("Control").layer("AWAYSTARTERS").text.sourceText;
//separate into individual players
players = full.split("\r");
//loop through to create a sub-array for each player
for (i = 0; i < players.length; i++) {
players = players.split(" ") }
//remove all but first character from first name if set to show initials
if (first == "initial") {
for (i = 0; i < players.length; i++) {
players[1] = players[1].split("",1);
}
}
//set output to blank
output = "";
//if showing first names, loop through array items and display
if (first == "initial" || first == "full") {
for (i = 0; i < players.length; i++) {
for (x = 0; x < players.length; x++) {
output = output + players
; if (x == (players.length - 1)) {
output = output + "\r"; }
else {output = output + " "};
}
}
}
//if not showing first names, loop through array skipping first name entry and display
else {
for (i = 0; i < players.length; i++) {
for (x = 0; x < players.length; x++) {
if (x == 1) continue;
output = output + players
; if (x == (players.length - 1)) {
output = output + "\r"; }
else {output = output + " "};
}
}
}
output;
It works perfectly if I have full names set to display, or set it to only display last names. But if I try to use initials, I get an 'invalid numerical result' on line 33.
If I change it to have a period before everyone's name (5. .CRAIG SAUNDERS, etc), it spits out "5 CRAWFORD" as the output. If change the period to a 2, the output is "15 CRAWFORD". Change it to a 7, and the result is "40 CRAWFORD".
What is going on? Where is the rest of the array disappearing to? Why does it think the initials are supposed to be numerically valid in the first place? If I comment out everything between 'output == "";' and the final closing bracket, and change the final line to output 'players[0][1]', it displays "C". Exactly as it should. Same is true all the way through the array.
Copy link to clipboard
Copied
Well, you're not really doing any sensible processing on line 33 - you are just counting something, not assembling a string, and a number is a number is a number, given that JS auto-converts string literals, numbers and variables. If you want to retain the numbers, you have to explicitly declare them as strings by putting them into quotation marks. Generally the whole approach is unnecessarily convoluted. Why even bother with your overly nested complicated loops? You could just as well split up everything and sort it into new arrays and then fetch the relevant info from there, which in fact will simplify your processing just as well. Nothing stops you from creating arrays from arrays, you know. It will also make it possible to reorder your output on various criteria, should that ever be necessary.
Mylenium
Copy link to clipboard
Copied
But the problem isn't the numbers, it's the initials. I can output '5. GEOFF SAUNDERS' just fine, and '5. SAUNDERS' just fine, but when I try to output '5. G SAUNDERS' it's suddenly not numerically valid. The problem only occurs when 'first' is set to 'initials'.
Although line 33 isn't merely counting, it's adding to the contents of the variable. I assume it tries to add "G" to "5." numerically, rather than...stringily?, and thus throws an error. Whereas when adding "GEOFF" to "5." it treats it as a string.
The reason I'm bothering with 'overly nested complicated loops' is that everything I know, I learned from Google, and this is the way I found to do the thing I want to do: Have one text layer with the five players, with their jersey number and full name, and then be able to present them in a number of different ways depending on the circumstance. I have code elsewhere that re-sorts them numerically (they're currently ordered by playing position) and presents them that way, for example.
Copy link to clipboard
Copied
Well, the way you wrote your code, your output variable could be empty in one of the loops because players.length -1 could be negative. I also don't quite get why you always do an output = output + something. Again, you are accumulating values, not processing strings. Rather than using this approach, I'd really use players.push(full) and build a new array (or concatenate another sring) and then use another simple loop to build the actual output strings. Once you have your arrays, it could be as simple as players.substr(1,1) to retrieve the initials and so on. Same for your math issue - use .toString() to convert your numbers to strings so they don't interfer with your counters.
Mylenium
Copy link to clipboard
Copied
I do the output that way because, like I said, it's the only way I know how to do it. I don't understand what you're saying about using players.push, building a new array, or concatenating another string.
I mean, as it is I end up with [[5,Geoff,Saunders] [10,Cal,Bruton]] etc, so the players are all in arrays...not sure how that differs from your suggestion?
I did try toString(players.[1].split("",1)), but that resulting in it printing as '[object Layer]'.
I replaced .split("",1) with .substr(0,1) and it now works. So something about the split was making it wonky. I've used substr() before, but I completely forgot about it.
Copy link to clipboard
Copied
Play around with this:
if (thisComp.layer("Null 18").effect("First Names")("Checkbox") == 1)
first = "full"
else if (thisComp.layer("Null 18").effect("First Initials")("Checkbox") == 1)
first = "initial"
else
first = "none";
full = comp("Control").layer("AWAYSTARTERS").text.sourceText;
players = full.split("\r");
outStr = "";
for (i = 0; i < players.length; i++){
player = players.split(" ");
line = player[0] + " ";
switch(first){
case "full":
line += player[1] + " ";
break;
case "initial":
line += player[1].substr(0,1) + " "
break;
case "none":
default:
break;
}
line += player[2];
outStr += line;
if (i != players.length-1) outStr += "\r";
}
outStr
Dan
Copy link to clipboard
Copied
Won't that cause someone like David Van Dyke to show up as either 'D Van' or just 'Van', though? I had something similar initially (without the switch(), and still using split() instead of substr()), manually pulling specific indexes, until I realised it would stop working for players with spaces in their last names (Van Dyke, Van Der Putten, Van Buuren...).
That's why I changed it to just loop through, that way it doesn't matter how many indexes there are after the first name, they all get displayed.
EDIT: Well the word filter on here seems a little overactive.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more