Gossamer Forum
Home : General : Perl Programming :

Understanding a regular Expression

Quote Reply
Understanding a regular Expression
 
Hi All ,

I have a small query. I have written a perl script that takes input from a configuration file.

The configuration file is like


MACHINE NAME = X
CONFIG = Y


I need to read this file and enter these values in a hash , as in ...

hash{'MACHINE NAME'}=X


I have seen a regular expression in one of the scripts , to identify the A=B pattern

It is :

if (($name,$value)= /^\s*([^=\s]+)\s*=\s*(.*?)\s*$/ ){

Just need to understand , how does this expression , preserves the value in $name and $ value variables

I understand some part though ...

/^\s* -> for starting whitespaces
([^=\s]+) --> ?? whats this ??
\s* -> white spaces before '='
=\s* -> white spaces after '='
(.*?) -> $value
\s* --> =\s* -> white spaces after $value
$/


Thanks
Quote Reply
Re: [gpta_varun] Understanding a regular Expression In reply to
The ([^=\s]+) bit is telling the regex to "break" the rule wehen it comes up with a = sign, and capture the result into $1

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Understanding a regular Expression In reply to
 
Thanks Andy ! You come to my rescue time and again !

I am stuck with another point now.

Below is the function that takes the config file as a parameter.

CONFIG file is something like :


[MACHINE NAMES]
A=1,2
B=3,4

** where 1,2,3,4 are the partitions on a machine 'A' or 'B'

I am creating a hash table (hash) , which stores data in the format

hash{MACHINE NAMES}{A}=1;

In the code , while $hash{$key_value}{$name} works , $hash{"MACHINE NAME"}{"A"} does not !!!
I would return the hash so formed to the main function and use it !



Code:

sub read_conf_file($)
{
my $i=0;
my $config_file=shift();
# store the attributes within [] in the key_table hash
my %hash=();
my $key_value;

print "******************\n";
open(CONFIG, "<$config_file") || die "I/O Success - Cannot process $config_file file";

while (<CONFIG>)
{
chomp;
if(/^\s*(#.*)$/){next;}

if(/^\s*\[(.*)\]$/)
{
$key_value=$1;
print $key_value;
#$key_table[$i++]=$1;
}
#elsif (/^\s*(?.*) \s*=\s*(?.*)\s*$/ )
else {
if(($name,$value)= /^\s*([^=\s]+)\s*=\s*(.*?)\s*$/ )
{
print "\n *** $name=$value ****\n";
$hash{$key_value}{$name}=$value;
print "\n *** $hash{$key_value}{$name}****\n"; --> WORKS
}
}


}
print "** ** ** IN FUNC ** ** ** \n";
print $hash{"MACHINE NAME"}{"A"}; --> not working
print "** ** ** IN FUNC ** ** ** \n";

return(\%hash);


}
Quote Reply
Re: [gpta_varun] Understanding a regular Expression In reply to
Also ,

With multiple machines A , B , C ..... etc ,

How would i dynamically grep values from the hash .... Need something like ...

hash {MACHINE NAME} { ....loop through all values }
Quote Reply
Re: [gpta_varun] Understanding a regular Expression In reply to
I hope this isn't coursework I'm helping you with Tongue

After:

Code:
print $hash{"MACHINE NAME"}{"A"};

try doing:

Code:
use Data::Dumper;
print Dumper($hash);

...then post the results (so I can see what kinda format the data is in)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [gpta_varun] Understanding a regular Expression In reply to
gpta_varun wrote:
Also ,

With multiple machines A , B , C ..... etc ,

How would i dynamically grep values from the hash .... Need something like ...

hash {MACHINE NAME} { ....loop through all values }

you would do something like:
Code:
if ($value =~ /,/) {
foreach (split /,/, $value) {
$hash{$key_value}{$name}=$_;
}
} else {
$hash{$key_value}{$name}=$value;
}

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Understanding a regular Expression In reply to
Result of Execution :

Quote:

*** A=1,2 ****

*** 1,2**** --> valid
Dump $VAR1 = '';
$VAR2 = {
'A' => '1,2'
};
** ** ** IN FUNC ** ** ** --> NO VALUES PRINTED HERE
** ** ** IN FUNC ** ** **



For the second part , I guess I have not been able to clarify things

I would maintain a hash , something like :

hash {MACHINE NAME}{A}='1,2'
hash {MACHINE NAME}{B}='3,4'


Once the hash is complete , I would return this to the main function

In the main function , for all machines A, B ..... I need to process all the partitions

However , the problem is I don't know the number of entries under the section [MACHINE NAMES]

I need to loop through all the machines A , B , C . Within a loop I'll try to split $value by ',' and process ....
Quote Reply
Re: [gpta_varun] Understanding a regular Expression In reply to
What is the value of your config file? I think you need to really do this a different way :) (you seem to be overcomplicating things :))

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Understanding a regular Expression In reply to
#MACHINE_NAME=<RESPECTIVE PARTITIONS NUMBERS>

[MACHINE NAME]
A=1,2
B=3,4

[GENERAL CONFIGURATIONS]
IP=1.2.3.4
Quote Reply
Re: [gpta_varun] Understanding a regular Expression In reply to
Untested, but try something like:


Code:
my $section;
my $vals;
open (READIT, "./filename.cfg") || die "Cant read filename.cfg. Reason: $!";

while (<READIT>) {
if ($_ =~ /^#/ || $_ =~ /^\s+/) { next; }

if ($_ =~ /\[(.*)\]/) {
$section = $1;
} else {
my ($col,$value) = split /=/, $_;
$vals->{$section}->{$col} = $value;
}
}
close (READIT);

..then call with something like:

Code:
$vals->{'MACHINE NAME'}->{"A"}

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Understanding a regular Expression In reply to
Thanks Andy !

Two questions :

1) 'vals' should be a hash right ?
2) Would I be able to graze through all the entries defined in $section = MACHINE NAMES ?
Quote Reply
Re: [gpta_varun] Understanding a regular Expression In reply to
$vals is a hashref :)

Quote:
2) Would I be able to graze through all the entries defined in $section = MACHINE NAMES ?

You could do it with something like:

Code:
my $tmp = $vals->{"MACHINE NAMES"};
map {
print qq|Column: $_ ----> VAL: $tmp->{$_}\n|
} keys %$tmp;

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Understanding a regular Expression In reply to
Its not working :(


Interesting part is that while print $hash{$key_value}{$name} works , (that means atleast the population of hash is OK , the $hash{'MACHINE NAME'}{'A'} does not !
Quote Reply
Re: [gpta_varun] Understanding a regular Expression In reply to
I'll test it locally later on - bit busy atm ;)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Understanding a regular Expression In reply to
oks !

Thanks Andy.
Quote Reply
Re: [gpta_varun] Understanding a regular Expression In reply to
Just tested it, and it works fine for me???

Code:
#!/usr/bin/perl

use Data::Dumper;

my $section;
my $vals;
open (READIT, "./test.cfg") || die "Cant read filename.cfg. Reason: $!";

while (<READIT>) {
if ($_ =~ /^#/ || $_ =~ /^\s+/) { next; }

if ($_ =~ /\[(.*)\]/) {
$section = $1;
} else {
my ($col,$value) = split /=/, $_;
$value =~ s/\s+$//g;
$vals->{$section}->{$col} = $value;
}
}
close (READIT);

print Dumper($vals);

print "FOO: " . $vals->{'MACHINE NAME'}->{'A'};

Gives:

Code:
perl test.cgi
$VAR1 = {
'GENERAL CONFIGURATIONS' => {
'IP' => '1.2.3.4'
},
'MACHINE NAME' => {
'A' => '1,2',
'B' => '3,4'
}
};
FOO: 1,2

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Understanding a regular Expression In reply to
Thanks Andy !

I compared the code that you had posted with the one that I created .

You won't believe , it was all due a small error in the regular expression used.

used
Code:
if(/^\s*\[(.*)\]/)
{
$key_value=$1;
chomp($key_value);
}

instead
Code:

if(/^\s*\[(.*)\]$/)
{
$key_value=$1;
chomp($key_value);
}

That created all the problem . Now working fine.


Here is how I cycled through the multidimensional hash

while(($machine,$partition_list) = each(%{$conf_hash->{"MACHINE NAME"}}))


Thanks again.

Regards
Varun
Quote Reply
Re: [gpta_varun] Understanding a regular Expression In reply to
Cool glad its working. That "little error" is one of the joys you get when working with any programming language <G> (although Perl is actually very good at returning helpful debugging error messages, compared to stuff like PHP, which I never really like the debugging options that gave you :()

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!