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
    This doesn't work
      can you prove it? rewrite sub subroutine so it starts with  my $records = ...; # Data::Dumper output here
        sub subroutine { my ($self,$vip) = @_; my @ip = (); my $sql_query = $self->{queryObj}->execute( "select primary_ip from buddy_vips where machineIP=$vip"); my $records = $self->{queryObj}->result(); for my $row (@$records){ if (my ($ip) = $row->{machineIP} =~ /(\d+\.\d+\.\d+\.\d+)/){ $row->{machineIP} = $ip; } push @ip, $row->{machineIP}; } return \@ip; }
        output :

        Use of uninitialized value in pattern match (m//) at

        Use of uninitialized value in pattern match (m//) at

        Use of uninitialized value in pattern match (m//) at