Tehillim 119:7 odcha beisher levav belamdi mishpetei tzidkecha
I will give thanks to You with an upright heart, when I learn Your just ordinances.
"I will give thanks to You with an upright heart," unlike those about whom it says, 'His mouth speaks and Him, but their heart is far from Him' (cf. Yeshayahu 29:13). Thus we "learn" from "Your just ordinances" that everything depends on intention. Hence the law of the city of refuge (BaMidbar 35) teaches that one who kills deliberately is executed, and one who kills unwittingly is exiled.
Because I have trained myself to accept "Your just ordinances," therefore "I give thanks to You" both for the good and for the bad, "with an upright heart." As our Sages have said, one is required to recite the blessing (also for the bad) with gladness. Thus Hillel once heard a voice of loud lament coming from his city, and he said, "I am certain that these are not my family lamenting." This is explained to mean that he had educated his family to accept everything with gladness and to not shriek.
Torah is the antidote for the yetzer harah (evil inclination). Torah study subdues the yetzer harah.
Tehillim 119:6
Tehillim 119:5
Tehillim 119:4
Tehillim 119:3
Tehillim 119:2
Tehillim 119:1
MeAm Lo'ez
Chazal