Doubt on matching

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pavanponnapalli
    New Member
    • May 2008
    • 51

    Doubt on matching

    hi,
    Here is my Code as under:
    Code:
           #! /usr/bin/perl -w
     
      use DBI;
      my %hash;
      my @arr;
      my @arr1;
      my @arr2;
      my $var;
      my $jn;
      my $vari;
      my @array;
      my @array1;
      my @array2;
      my @array3;
      #Variable Declaration
        my $path = "D:/pavan/muco.log";
    	#definition of DataBase variables
     	my $db="ASP_WebStats";
     	my $host="192.168.0.2";
     	my $user="jivausers";
     	my $password="jivausers";
     	
     
     	          
     	  @arr1 = sub_loc();
     	 print "------------------------------------------------------ \n";
     	 print "@arr1 \n";
     	 print "--------------------------------------------------------- \n";
     	 my $count = scalar(@arr1);
     	 #
     	 #print "<$count> \n";
     	 #print "($arr1[5])";
     	 foreach(@arr1)
     	 {
     	 	  if($_=~/[\n,\s]/)
     	 	  {
     	 	  	     #print "<<$`>>\t<$'>\n";
     	 	  	     push(@array1,$`,$');
     	 	  }
     	 	  #print "$_";
     	 }
     	 
     	 my $count1 = scalar(@array1);
     	 for($i=0;$i<$count1;$i++)
     	 {
     	 	my $join_var = join (" ",$array1[$i],$array1[++$i]);
     	 #	print "((($join_var))) \n";
     	 	push(@array2,$join_var);
     	 }
      #   print "!!!$array2[0]!!!";
         
         #my $dum = scalar(@array2);
         foreach(@array2)
         {
         	if($_=~/(\w)*(\s)*/)
         	{	
         		#print "$'\n";
         		push(@array3,$');
         	}
         	else
         	{
         		print "The Pattern does not match the given string";
         	}
         }
         
         print "+++@array3+++\n";
         foreach(@array3)
         {   	 	????????? 
         }
     	 print "\n\n\n\n";
    	#connect to MySQL database
    	my $dbh=DBI->connect ("DBI:mysql:database=$db:host=$host",
    	                           $user,
    	                           $password) 
    	                           or die "Can't connect to database: $DBI::errstr\n";
      	                           
      $sth = $dbh->prepare("select * from Product order by Product_Id") or warn "Cannot Prepare: $DBI::errstr \n";
      $sth->execute(); #Execute statement Handler 
      
      my $dump_reference = $sth->fetchall_arrayref;
      
      foreach $dval (@$dump_reference)
      {
        print "---------------------------------------------------------------- \n";
      	print "Product ID: @$dval[0] \n";
      	print "Product Code: @$dval[1] \n";
      	print "Product Name: @$dval[2] \n";
      	print "Product Type: @$dval[3] \n";
      	print "Created By: @$dval[4] \n";
      	print "Created DTM: @$dval[5] \n";
      	print "Modified By: @$dval[6] \n";
      	print "Modified Dtm: @$dval[7] \n"; 
      	print "------------------------------------------------- \n";
      	@arr=();
      }
    
    
      
      
      
      sub sub_loc
      {
      	@array=();
      #open a filehandler in readmode
          open(FH, "<$path");
    
     while(<FH>)   #loop through the entire contents of the file
     {             # Open Brace of a while loop
    	#print "<$_>\n";  
    	
    	#if $_ contains 0 or multiple words followed by a single or multiple spaces followed by any of the characters below
    	if($_=~/(\w)*(\s)*[\{,\(,\-,\+,\=,\^,\!,\%,\<,\>,\?,\/,\&]/) 
    	{
    	      #print "$&";
    	      #print ">>>>>$'<<<<<"; #prints the string after match
    	      
              #print "$var \n";
              
              $jn = join(" ",$&,$'); #Joins the matched part with the one after match with space
              
              #print "(((((($jn))))))";
    	      push(my @arr,$jn);  # push the joined values in a list
    	     # print "<@arr>";
    	      #print "<<@arr>> \n";
    	      foreach(@arr)      #Loop through the entire list
    	      {
    	      	 if($_=~/[\",\(,\)]/) #If that list contains " or ( etc 
    	      	 {
    	      	 	$_=~ s/[",(,),},{]//g;  #Removes them
    	 #     	 	print "*************$_*************** \n\n";
    	      	 	push(@array,$_);    #Pushes the _ contents in a list
    	      	 } #close if
    	      } #close foreach
    	} #close if
    	     #return @array;
       }
            return (@array);           #Returns the entire list
      }  #close sub
    The log files for this program are as under:
    Code:
     TURNAWAY (type="video" product="opiv" session="foo12345" customer="muco:1234" time="1234567890")
    CUSTOMER {customer_id="muco:443" name="Yale University"}
    PLAYBACK (type="audio" product="glmu" customer_id="muco:443" session="muco:foo12345abcde" user_id="muco:123" identifier="muco:123456" publisher="EMI" title="Merengue from Music of Guatemala, Vol. 1" played="180" timestamp="1234567890" token="abcdef1234567890abcdef")
    HTTPLOG (format="apache" product="cpho" customer="123")
    I have made log simple as under with my code:
    Code:
    TURNAWAY  type=video product=opiv session=foo12345 customer=muco:1234 time=1234567890
     CUSTOMER  customer_id=muco:443 name=Yale University
     PLAYBACK  type=audio product=glmu customer_id=muco:443 session=muco:foo12345abcde user_id=muco:123 identifier=muco:123456 publisher=EMI title=Merengue from Music of Guatemala Vol. 1 played=180 timestamp=1234567890 token=abcdef1234567890abcdef
     HTTPLOG  format=apache product=cpho customer=123
    In the code above i have put ??????? . There i am trying to match = and split them to store them into the database. I have used
    Code:
     foreach(@array3)
    {
         if(s/\=/g)
         { 
            print "$`";
            print "$';
         }
     but only first '=' from a line is matching.\g is not working there. Can u plesase help me?
    
    Thanks,
    pavan
    }
    Last edited by eWish; Aug 23 '08, 01:46 PM. Reason: Fixed code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    try removing the "s":

    Code:
    if(s/\=/g)
    like this:

    Code:
    if(/=/g)
    the "s" is telling perl you want to do a substitution, and the code should throw an error because it is not a properly constructed regexp (s/pattern/substitution/).

    Removing the "s" defaults it to a matching regexp (m/pattern/);

    Comment

    • sasimca007
      New Member
      • Sep 2007
      • 129

      #3
      Code:
      open FH,"<test.txt";
      while(<FH>)
      {
      	my $data = $_;	#Assigning line to the variable $data
      	print "$data\n";
      	while((length($data)) != 1)	#unless $data is not equal to ) or } or ] or etc......
      	{
      		if($data =~ /(\w*)\=\"/)	#matching word="
      		{
      			my $dt = $';	#Assigning After matched to $dt
      			$key = $1;
      			print "$key=";	#printing the key
      			if($dt =~ /\"/)	#Matching end of a value with "
      			{
      				print "$`\n";	#printing the value
      #				$value=$`;
      #				$sth = $dbh->prepare("select * from product where product_code='$value'");
      #				$resp = $sth->execute();
      #				if($resp ne '0E0')
      #				{
      #					$sth1 = $dbh->do("insert into sometable values('$value')");
      #				}
      				$data = $';	#Assigning the after matched to $data
      				$data =~ s/^\s//;	#Removing first space
      				chomp($data);	#Removing newline character of $data
      			}
      		}
      	}
      	print "\n";
      }
      close(FH);

      Comment

      • sasimca007
        New Member
        • Sep 2007
        • 129

        #4
        Code:
        my %hash = ();
        open FH,"<test.txt";
        while(<FH>)
        {
        	my $data = $_;	#Assigning line to the variable $data
        	print "$data\n";
        	if($data =~ /^(\w*)/)	#To get first word of line
        	{	print "$1\n";$main = $1;	}
        	while((length($data)) != 1)	#unless $data is not equal to ) or } or ] or etc......
        	{
        		if($data =~ /(\w*)\=\"/)	#matching word="
        		{
        			my $dt = $';	#Assigning After matched to $dt
        			$key = $1;
        			print "$key=";	#printing the key
        			if($dt =~ /\"/)	#Matching end of a value with "
        			{
        				print "$`\n";	#printing the value
        				$hash{$main}{$key} = $`;	#Building hashes of hashes
        #				$value=$`;
        #				$sth = $dbh->prepare("select * from product where product_code='$value'");
        #				$resp = $sth->execute();
        #				if($resp ne '0E0')
        #				{
        #					$sth1 = $dbh->do("insert into sometable values('$value')");
        #				}
        				$data = $';	#Assigning the after matched to $data
        				$data =~ s/^\s//;	#Removing first space
        				chomp($data);	#Removing newline character of $data
        			}
        		}
        	}
        	print "\n";
        }
        close(FH);
        #printing the hash
        foreach$s(keys %hash)
        {
        	print "$s\n";
        	foreach(keys %{$hash{$s}})
        	{
        		print "\t$_ <> $hash{$s}{$_}\n";
        	}
        	print "\n";
        }

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          sasimca007,

          I don't know about anyone else, but a whole lot of code, without any explanation is not the best kind of help. It would be better to point out where the OP has errors and such in there code instead of just freely providing them a solution to their problem.

          The idea is for those asking questions to learn and by just giving them every answer freely does not always do that. Please don't get me wrong, thank you very much for helping and providing a solution, but be sure to help the OP learn and understand rather than just feeding them code.

          Regards,

          Jeff

          Comment

          Working...