R
robic0
What is the conventional way? Question of weather re.dll is opened at compile time,Anno said:You're using the wrong tool.
Why would split() be wrong?
The rule (known as Randal's rule) is "If you know what to throw away,
use split. If you know what to keep, use a capturing regex."
I agree that's a good rule of thumb, but...
In your case, what you want to keep it the digits at the end of each line.
And what the OP wants to throw away is everything but those digits.
You can get that by throwing away blanks and then accessing the n-th word
(4-th in the first case, 8-th in the second), but that's really roundabout.
Assuming a line in $_,
my ( $num) = /(\d+)$/;
will extract the number in both cases (untested).
my $num = (split)[-1];
Roundabout? To me it appears to be just as simple to figure out and
type. And isn't it actually faster to use the optimized split() rather
than invoking the regex engine in the conventional way?
depends on the complexity and if the simple built in parser can handle it or not.
The unknowing OP wants, as it appears to you, to grab an totally meaningless capture
of a, huh (?), *number* at the end of each line.
As totally meaningless the thought of grabbing a single number from lines from
a data file, and making an array out of it, you will extract the last item produced by
the split "my $num = (split)[-1];" (is that what its doing?).
That results in a unqualified, meaningless monolithic result not referencable anyway.
Hey, but maybe thats why "split" is good for you..