473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
4 1553
KevinADC
4,059 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
sasimca007
129 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
sasimca007
129 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
3,509 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

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

Similar topics

176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
12
by: Simone Mehta | last post by:
hi All, I am parsing a CSV file. I want to read every row into a char array of reasonable size and then extract strings from it. <snippet> char foo="hello,world,bye,bye,world"; ........
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
1
by: solarin | last post by:
Hi, I've developed a program under VS 6.0. I can compile it and run it, but when I try to debbug , all my breakpoints are dissabled and I can see the following messages: Loaded...
6
by: abctech | last post by:
My requirement is such that any user must not be allowed to save the same record twice. For this what I do is as soon as some user fills up the page and clicks 'Save' I invoke a Servlet which...
2
by: cviniciusm | last post by:
Hello, I'm using "g++ (GCC) 4.1.1 20070105 (Red Hat 4.1.1-51)". I'm trying to use the open() function as defined by the header fcntl.h, but I'm getting this error: " mylibrary.cpp:16: error:...
2
by: santhoshs | last post by:
Hello I am required to parse two files that contain email addresses and figure out a way to get the matching and non-matching email addresses from both the files. I was able to get the matching...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
1
by: sora | last post by:
Hi, I've developed a MFC program under VS 6.0. My debugger *was* working fine and I've used it often for my project. Then, one day, the errors below appear and they prevent me from using the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.