in reply to A little config parsing.

I think split is your friend in this case. Perhaps something along the lines of my ($key,$operator,$value) = split /\s*(=>?)\s*/,$_,2; will set you in the right direction. The rest is just trimming leading and trailing whitespace.

Update: In the interests of TIMTOWDI, I do believe that one can modify your above regex to the following: /(\S.*?)\s*(\=\>?)\s*(.+?)\s*/ However, due to the amount of backtracking involved in the regex, along with the general ugliness of it, I'd personally use split in this case. In my mind, it also better captures the essence of what you're trying to do -- you've got these two things, and they'er seperated by something. You want to split them apart, so..

perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Replies are listed 'Best First'.
Re: Re: A little config parsing.
by rendler (Pilgrim) on Jan 11, 2002 at 08:05 UTC
    Thanks that worked great, but then I went to test the results with
    for my $i (1 .. $sites_config{'total_sites'}) { print "$sites_config{$i}{'site_name'}\n"; }
    What I don't get is how $i is getting auto incremented.

      $i is the loop variable for your for loop. Perl sets it to every value in the list that the loop is iterating over -- in this case, the list of numbers from 1 to $sites_config{'total_sites'}.

      As an aside, any particular reason you're using a hash and not an array? It seems like you're mixing metaphors a bit to have </code>$site_config{'some_string'}</code> be at the same level as $site_config{$some_site_number}.

      perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

        I find it easier to work with just one hash to store whatever I need than to have multiple arrays to store the same thing and try and guess the number of where something is stored to retrive it.