in reply to regex in perl
Untested. It'll push the IPv6 addresses onto @ip, but if there's a v4 address in an entry, it'll grab just the four v4 octets and will push only the x.x.x.x:
for my $row (@$records){ if (my ($ip) = $row->{machineIP} =~ /(\d+\.\d+\.\d+\.\d+)/){ $row->{machineIP} = $ip; } push @ip, $row->{machineIP}; }
If a $row->{machineIP} matches the regex, we capture just the x.x.x.x portion with the () capture group (ignoring everything else in the string), and assigns it to $ip. We need parens around $ip because if we didn't have them, we'd be assigned to in scalar context, and would get the count of matches. () forces list context, so we get the actual matches, not just the count.
If there's no match, we just push the db row entry onto @ip as-is.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: regex in perl
by user786 (Sexton) on Jun 15, 2016 at 03:18 UTC | |
by Anonymous Monk on Jun 15, 2016 at 03:27 UTC | |
by user786 (Sexton) on Jun 15, 2016 at 03:40 UTC | |
by afoken (Chancellor) on Jun 15, 2016 at 05:58 UTC | |
by Anonymous Monk on Jun 15, 2016 at 03:44 UTC | |
by stevieb (Canon) on Jun 15, 2016 at 12:24 UTC |