ST. LOUIS —Nolan Arenado and Iván Herrera each had three hits, Sonny Gray pitched seven strong innings, and the St. Louis Cardinals beat the Pittsburgh Pirates 4-2 on Wednesday night.

Arenado scored a run and drove in one while Herrera also scored a run. Brendan Donovan added two hits and an RBI.

“If I stay locked in on both sides of the ball, I feel like I can help this team win a lot of games,” Arenado said. “That’s all I’m focused on.”

Gray (8-4) gave up one run and four hits while striking out nine and walking just one. He had lost his previous two starts.

“It was time to put up or shut up,” Gray said. “I had a mentality shift throughout the week. I want to continue down this road. I write in my little book here, pitch to win the game and not worry about anything else. When you do that, good things tend to happen.”

In the first inning, Gray walked the leadoff batter and gave up a double before striking out three.

“That was fun to watch,” St. Louis manager Oliver Marmol said. “To get out that was pretty gritty. You could see the momentum after that. He was taking care of business.”

Pittsburgh manager Derek Shelton said his team missed an opportunity there in the first against Gray.

“We saw him out of the zone to the first two hitters and then after that, you see why Sonny Gray’s an ace to staff, ” Shelton said. “He has the ability to execute. Overall, after the first two hitters, he was extremely sharp.”

Ryan Helsley gave up a leadoff single in the ninth before finishing up for his major league-leading 22nd save in 23 chances.