467,081 Members | 1,079 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

Doubt on matching

hi,
Here is my Code as under:
Expand|Select|Wrap|Line Numbers
  1.        #! /usr/bin/perl -w
  2.  
  3.   use DBI;
  4.   my %hash;
  5.   my @arr;
  6.   my @arr1;
  7.   my @arr2;
  8.   my $var;
  9.   my $jn;
  10.   my $vari;
  11.   my @array;
  12.   my @array1;
  13.   my @array2;
  14.   my @array3;
  15.   #Variable Declaration
  16.     my $path = "D:/pavan/muco.log";
  17.     #definition of DataBase variables
  18.      my $db="ASP_WebStats";
  19.      my $host="192.168.0.2";
  20.      my $user="jivausers";
  21.      my $password="jivausers";
  22.  
  23.  
  24.  
  25.        @arr1 = sub_loc();
  26.       print "------------------------------------------------------ \n";
  27.       print "@arr1 \n";
  28.       print "--------------------------------------------------------- \n";
  29.       my $count = scalar(@arr1);
  30.       #
  31.       #print "<$count> \n";
  32.       #print "($arr1[5])";
  33.       foreach(@arr1)
  34.       {
  35.             if($_=~/[\n,\s]/)
  36.             {
  37.                      #print "<<$`>>\t<$'>\n";
  38.                      push(@array1,$`,$');
  39.             }
  40.             #print "$_";
  41.       }
  42.  
  43.       my $count1 = scalar(@array1);
  44.       for($i=0;$i<$count1;$i++)
  45.       {
  46.           my $join_var = join (" ",$array1[$i],$array1[++$i]);
  47.       #    print "((($join_var))) \n";
  48.           push(@array2,$join_var);
  49.       }
  50.   #   print "!!!$array2[0]!!!";
  51.  
  52.      #my $dum = scalar(@array2);
  53.      foreach(@array2)
  54.      {
  55.          if($_=~/(\w)*(\s)*/)
  56.          {    
  57.              #print "$'\n";
  58.              push(@array3,$');
  59.          }
  60.          else
  61.          {
  62.              print "The Pattern does not match the given string";
  63.          }
  64.      }
  65.  
  66.      print "+++@array3+++\n";
  67.      foreach(@array3)
  68.      {            ????????? 
  69.      }
  70.       print "\n\n\n\n";
  71.     #connect to MySQL database
  72.     my $dbh=DBI->connect ("DBI:mysql:database=$db:host=$host",
  73.                                $user,
  74.                                $password) 
  75.                                or die "Can't connect to database: $DBI::errstr\n";
  76.  
  77.   $sth = $dbh->prepare("select * from Product order by Product_Id") or warn "Cannot Prepare: $DBI::errstr \n";
  78.   $sth->execute(); #Execute statement Handler 
  79.  
  80.   my $dump_reference = $sth->fetchall_arrayref;
  81.  
  82.   foreach $dval (@$dump_reference)
  83.   {
  84.     print "---------------------------------------------------------------- \n";
  85.       print "Product ID: @$dval[0] \n";
  86.       print "Product Code: @$dval[1] \n";
  87.       print "Product Name: @$dval[2] \n";
  88.       print "Product Type: @$dval[3] \n";
  89.       print "Created By: @$dval[4] \n";
  90.       print "Created DTM: @$dval[5] \n";
  91.       print "Modified By: @$dval[6] \n";
  92.       print "Modified Dtm: @$dval[7] \n"; 
  93.       print "------------------------------------------------- \n";
  94.       @arr=();
  95.   }
  96.  
  97.  
  98.  
  99.  
  100.  
  101.   sub sub_loc
  102.   {
  103.       @array=();
  104.   #open a filehandler in readmode
  105.       open(FH, "<$path");
  106.  
  107.  while(<FH>)   #loop through the entire contents of the file
  108.  {             # Open Brace of a while loop
  109.     #print "<$_>\n";  
  110.  
  111.     #if $_ contains 0 or multiple words followed by a single or multiple spaces followed by any of the characters below
  112.     if($_=~/(\w)*(\s)*[\{,\(,\-,\+,\=,\^,\!,\%,\<,\>,\?,\/,\&]/) 
  113.     {
  114.           #print "$&";
  115.           #print ">>>>>$'<<<<<"; #prints the string after match
  116.  
  117.           #print "$var \n";
  118.  
  119.           $jn = join(" ",$&,$'); #Joins the matched part with the one after match with space
  120.  
  121.           #print "(((((($jn))))))";
  122.           push(my @arr,$jn);  # push the joined values in a list
  123.          # print "<@arr>";
  124.           #print "<<@arr>> \n";
  125.           foreach(@arr)      #Loop through the entire list
  126.           {
  127.                if($_=~/[\",\(,\)]/) #If that list contains " or ( etc 
  128.                {
  129.                    $_=~ s/[",(,),},{]//g;  #Removes them
  130.      #              print "*************$_*************** \n\n";
  131.                    push(@array,$_);    #Pushes the _ contents in a list
  132.                } #close if
  133.           } #close foreach
  134.     } #close if
  135.          #return @array;
  136.    }
  137.         return (@array);           #Returns the entire list
  138.   }  #close sub
  139.  
The log files for this program are as under:
Expand|Select|Wrap|Line Numbers
  1.  TURNAWAY (type="video" product="opiv" session="foo12345" customer="muco:1234" time="1234567890")
  2. CUSTOMER {customer_id="muco:443" name="Yale University"}
  3. 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")
  4. HTTPLOG (format="apache" product="cpho" customer="123")
I have made log simple as under with my code:
Expand|Select|Wrap|Line Numbers
  1. TURNAWAY  type=video product=opiv session=foo12345 customer=muco:1234 time=1234567890
  2.  CUSTOMER  customer_id=muco:443 name=Yale University
  3.  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
  4.  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
Expand|Select|Wrap|Line Numbers
  1.  foreach(@array3)
  2. {
  3.      if(s/\=/g)
  4.      { 
  5.         print "$`";
  6.         print "$';
  7.      }
  8.  but only first '=' from a line is matching.\g is not working there. Can u plesase help me?
  9.  
  10. Thanks,
  11. pavan
  12. }
  13.  
Aug 23 '08 #1
  • viewed: 1279
Share:
4 Replies
KevinADC
Expert 2GB
try removing the "s":

Expand|Select|Wrap|Line Numbers
  1. if(s/\=/g)
like this:

Expand|Select|Wrap|Line Numbers
  1. 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/);
Aug 23 '08 #2
100+
Expand|Select|Wrap|Line Numbers
  1. open FH,"<test.txt";
  2. while(<FH>)
  3. {
  4.     my $data = $_;    #Assigning line to the variable $data
  5.     print "$data\n";
  6.     while((length($data)) != 1)    #unless $data is not equal to ) or } or ] or etc......
  7.     {
  8.         if($data =~ /(\w*)\=\"/)    #matching word="
  9.         {
  10.             my $dt = $';    #Assigning After matched to $dt
  11.             $key = $1;
  12.             print "$key=";    #printing the key
  13.             if($dt =~ /\"/)    #Matching end of a value with "
  14.             {
  15.                 print "$`\n";    #printing the value
  16. #                $value=$`;
  17. #                $sth = $dbh->prepare("select * from product where product_code='$value'");
  18. #                $resp = $sth->execute();
  19. #                if($resp ne '0E0')
  20. #                {
  21. #                    $sth1 = $dbh->do("insert into sometable values('$value')");
  22. #                }
  23.                 $data = $';    #Assigning the after matched to $data
  24.                 $data =~ s/^\s//;    #Removing first space
  25.                 chomp($data);    #Removing newline character of $data
  26.             }
  27.         }
  28.     }
  29.     print "\n";
  30. }
  31. close(FH);
Aug 26 '08 #3
100+
Expand|Select|Wrap|Line Numbers
  1.  
  2. my %hash = ();
  3. open FH,"<test.txt";
  4. while(<FH>)
  5. {
  6.     my $data = $_;    #Assigning line to the variable $data
  7.     print "$data\n";
  8.     if($data =~ /^(\w*)/)    #To get first word of line
  9.     {    print "$1\n";$main = $1;    }
  10.     while((length($data)) != 1)    #unless $data is not equal to ) or } or ] or etc......
  11.     {
  12.         if($data =~ /(\w*)\=\"/)    #matching word="
  13.         {
  14.             my $dt = $';    #Assigning After matched to $dt
  15.             $key = $1;
  16.             print "$key=";    #printing the key
  17.             if($dt =~ /\"/)    #Matching end of a value with "
  18.             {
  19.                 print "$`\n";    #printing the value
  20.                 $hash{$main}{$key} = $`;    #Building hashes of hashes
  21. #                $value=$`;
  22. #                $sth = $dbh->prepare("select * from product where product_code='$value'");
  23. #                $resp = $sth->execute();
  24. #                if($resp ne '0E0')
  25. #                {
  26. #                    $sth1 = $dbh->do("insert into sometable values('$value')");
  27. #                }
  28.                 $data = $';    #Assigning the after matched to $data
  29.                 $data =~ s/^\s//;    #Removing first space
  30.                 chomp($data);    #Removing newline character of $data
  31.             }
  32.         }
  33.     }
  34.     print "\n";
  35. }
  36. close(FH);
  37. #printing the hash
  38. foreach$s(keys %hash)
  39. {
  40.     print "$s\n";
  41.     foreach(keys %{$hash{$s}})
  42.     {
  43.         print "\t$_ <> $hash{$s}{$_}\n";
  44.     }
  45.     print "\n";
  46. }
  47.  
Aug 29 '08 #4
numberwhun
Expert Mod 2GB
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
Aug 29 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

176 posts views Thread by Thomas Reichelt | last post: by
12 posts views Thread by Simone Mehta | last post: by
5 posts views Thread by olaufr@gmail.com | last post: by
1 post views Thread by solarin | last post: by
2 posts views Thread by cviniciusm@gmail.com | last post: by
11 posts views Thread by tech | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.