Discuss Scratch

Hockeycat88
Scratcher
48 posts

Lists (Please help!)

Hello scratchers! I have a question regarding lists.

For a project of mine, I have a list inside it. There are 20 lines of data, each with a number. Here's an example:

Line 1: 7
2: 6
3: 7
4: 8
5: 7
6: 6
7: 9
8: 6
9: 10
10: 8
11: 7
12: 9
13: 8
14: 6
15: 8
16: 9
17: 7
18: 8
19: 6
20: 7

How can I code a script that will tell me which line has the largest value (in this case it will be line 9)? In other words, a script that will look at the list and tell me that Line 9 has the largest amount. I'm not looking for the highest value across the list, but the line that has that number.

Scratch on!
Steve0Greatness
Scratcher
1000+ posts

Lists (Please help!)

Maybe create a loop to loop through all items of the list, and add the biggest one to a variable.
Paddle2See
Scratch Team
1000+ posts

Lists (Please help!)

Well, you will need a couple of variables:

Index_of_Largest_Value
Index

Initialize Index to 1 and Index_of_Largest_Value also to 1 and then loop Index from 2 to 20, comparing the value of the list at Index to the value at Index_of_Largest_Value. If it is larger, update Index_of_Largest_Value.

Of course, you can name the variables anything you want.

Last edited by Paddle2See (May 26, 2022 09:08:42)

Steve0Greatness
Scratcher
1000+ posts

Lists (Please help!)

Here's an example of something you could do here:
define greatest value
set [index v] to (1)
set [greatest value v] to (0)
repeat (length of [list v] :: list)
if <(item (index) of [list v] :: list) > (greatest value)> then
set [greatest value v] to (item (index) of [list v] :: list)
end
end

Powered by DjangoBB