Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What is your configuration data structure supposed to look like? So far the solutions only record the last line because they are stomping on the same hash keys every time they add a record. </p> <p>Here's something that might get you closer, but you still need to figure out what the data structure should be.</p> <ul> <li><p>I pass in the file handle as an argument so my subroutine isn't tied to a particular way of getting the data. It can be from a file, a string, a socket, or even the stuff below <strong>DATA</strong> in this case.</p></li> <li><p>Instead of fixing things up after I parse the string, I fix the string to have the "ip" element before I parse it. Once I do that, the "ip" element isn't a special case and it's just a matter of a double split. This is a very important technique to save a lot of work and code.</p></li> <li><p>I create a hash reference inside the subroutine and return that hash reference when I'm done. I don't need a global variable. :)</p></li> </ul> <pre> use warnings; use strict; use Data::Dumper; readConfigFile( \*DATA ); sub readConfigFile { my( $fh ) = shift; my $hash = {}; while( &lt;$fh> ) { chomp; s/\s+(\d*\.\d+)$/>ip=$1/; $hash->{ $. } = { map { split /=/ } split />/ }; } return $hash; } my $hash = readConfigFile( \*DATA ); print Dumper( $hash ); __DATA__ pub=3>rec=0>size=3>adv=1234 123 4.5 6.00 pub=1>rec=1>size=2>adv=111 22 3456 .76 </pre> <p>This gives you a data structure where each line is a separate record. I choose the line number of the record (<code>$.</code>) as the top-level key, but you can use anything that you like.</p> <pre> $VAR1 = { '1' => { 'ip' => '6.00', 'rec' => '0', 'adv' => '1234 123 4.5', 'pub' => '3', 'size' => '3' }, '2' => { 'ip' => '.76', 'rec' => '1', 'adv' => '111 22 3456', 'pub' => '1', 'size' => '2' } }; </pre> <p>If that's not the structure you want, show us what you'd like to end up with and we can adjust our answers.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload