Have a logfile my_file.txt
which I know has the following structure:
milk marta 1231 joe 1223 ralph 7C3D
cookie
jam
milk marta 163 joe 23 ralph 7FFF
And what I want to do is extract with the help of regex in perl all the values of lines that start with the delimiter milk
(which I know beforehand). Then I know I will have a number of value names (in the toy example marta
, joe
, ralph
), each followed by a decimal or hexadecimal value.
Is there a way to read the values without knowing beforehand if they are decimal or hex?
What is the best way to store this values? I thought of doing a hash with the keys being
marta
,joe
,ralph
, but I might not know them beforehand, meaning I will know all of them once I have read the first line, not before.
And here comes my attempt so far
$my_file = "my_file.txt"
open my $fh, $my_file or die "Nope!"
while (my $line = <$fh>)
{
if ($line =~ m/milk/)
{
# here iterate with a for or while loop over the $line
# and read word and value
}
}