Hi - can't work this one out, which is frustrating me !

Trying to grab a subset of data from the data input - essentially, each network element and once I have that network stored in the array, check (grep) if the array contains the string 'dhcp-range', and if it does - do stuff.

I think I'm getting the 'Use of uninitialized value $_' error on the 2nd & 3rd end of network data lines ('</ip4-network>'), as those network elements stored in the array don't contain the 'dhcp-range' string. But ... I don't get why it's throwing the error !

On a side note, the data is XML, and I tried using XML::Simple to do stuff, but i want to diff two files in the end, and XML::Simple changed the data structure, so i reverted to this way instead

#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use 5.010; # declare my @net_configs; my @data = <DATA>; foreach my $confLine (@data) { # find start of a network, push to array if ( $confLine =~ /<ip4-network range/ ) { say "Found start of network !"; push( @net_configs, $confLine ); } # push other network lines onto array elsif ( @net_configs ) { push( @net_configs, $confLine ); # check for end of a network if ( $confLine =~ /<\/ip4-network>/ ) { say "Found end of network !"; if ( grep /dhcp-range/, @net_configs ) { say "Found a dhcp-range !"; foreach my $net_config (@net_configs) { print $net_config; } } @net_configs = undef; } } } __DATA__ <data> <configuration name="Test"> <ip4-network range="10.2.2.0/24"> <dhcp-range range="10.2.2.50 - 10.2.2.99"/> <dhcp-service-option name="next-server" value="10.1.2.3"/> <dhcp-client-option name="tftp-server-name" code="66" valu +e="10.1.2.3"/> <dhcp-client-option name="boot-file-name" code="67" value= +"boot.com"/> </ip4-network> <ip4-network range="10.2.4.0/24"> <ip4-address address="10.2.4.2" name="10.2.4.2" state="dhc +p-reserved" mac="AA-BB-CC-DD-EE-FF"/> <dhcp-service-option name="next-server" value="10.1.2.3"/> <dhcp-client-option name="tftp-server-name" code="66" valu +e="10.1.2.3"/> <dhcp-client-option name="boot-file-name" code="67" value= +"boot.com"/> </ip4-network> <ip4-network range="192.168.1.0/24"> <dhcp-service-option name="next-server" value="10.1.2.3"/> <dhcp-client-option name="tftp-server-name" code="66" valu +e="10.1.2.3"/> <dhcp-client-option name="boot-file-name" code="67" value= +"boot.com"/> </ip4-network> </configuration> </data>
Output:
Found start of network ! Found end of network ! Found a dhcp-range ! <ip4-network range="10.2.2.0/24"> <dhcp-range range="10.2.2.50 - 10.2.2.99"/> <dhcp-service-option name="next-server" value="10.1.2.3"/> <dhcp-client-option name="tftp-server-name" code="66" valu +e="10.1.2.3"/> <dhcp-client-option name="boot-file-name" code="67" value= +"boot.com"/> </ip4-network> Found start of network ! Found end of network ! Use of uninitialized value $_ in pattern match (m//) at zTweak.pl line + 32, <DATA> line 21. Found start of network ! Found end of network ! Use of uninitialized value $_ in pattern match (m//) at zTweak.pl line + 32, <DATA> line 21.

Thanks!


In reply to Use of uninitialized value $_ in pattern match (m//) by stroke

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.