in reply to Re^9: Converting Oracle report language code into perl
in thread Converting Oracle report language code into perl

yeah I know ...here my code
$ARGV[0] .= ".rpt"; open(FH,"<$ARGV[0]") or die "Cant open"; @arr = <FH>; close(FH); foreach $data(@arr){ if($data =~ /^\.define/ .. /^\.\.\.define$/){ $data =~ s/(^\.\w+\s+)(\w+)(\s+)(.+\w+$)(\s+\. \.$)/$2,$4/smx; print "$data \n"; } }
and I am getting the output like this
.define lok_tabs lock tab book_tab, book_mat, custdata2, ed_tab, book_rates, print_tab in share update mode .. .define get_input select key, key1, key2, key3, key4, print_name into input_book_seq, fax_head, file_num, in_print_rates, NotId, print_name from ed_table where ed_table.tag = 'BOOK' and ed_table.key = 'PRNT' and ed_table.user_id = user ..
u can see I am not able to capture the data between .define and .. properly using the regex..PLease help me out

Replies are listed 'Best First'.
Re^11: Converting Oracle report language code into perl
by BrowserUk (Patriarch) on Jun 27, 2007 at 13:00 UTC

    When you did @arr = <FH>;, you broke your file into an array of lines. One per array element.

    When you do foreach $data(@arr){, you are setting $data to each of those lines in turn. Ie. $data will only ever contain one line at a time.

    So, when you do $data =~ s/(^\.\w+\s+)(\w+)(\s+)(.+\w+$)(\s+\. \.$)/$2,$4/smx;, which is attempting to match this:

    .define lok_tabs lock tab book_tab, book_mat, custdata2, ed_tab, book_rates, print_tab in share update mode ..

    which is spread over 4 lines, it will always fail.

    How could it match 4 lines in one line?

    Do you have any ideas for how you might get around this problem?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      This is the point where I am getting stuck actually...and .define is not a single statement ...There are lot of in this file...Please suggest some solution

        Sorry, but you have exhausted my patience also.

        CountZero posted a working solution to your problem and you have basically ignored it. All you have to do is add a couple of lines of code to that program to open the file and change one line and it does everything you have asked for and more.

        Instead, you just keep posting the same few lines of broken code and asking the same question over and over, whilst making no attempt to understand what you are being told and offered.

        Here is CountZero's program with two lines from your code added and one small change:

        #! perl use strict; use Data::Dumper ; my %defines; my $flag_in_define = 0; my $name; $ARGV[0] .= ".rpt"; open(FH,"<$ARGV[0]") or die "Cant open"; MAIN_LOOP: while (<FH>) { if (m/^\.define (.*)/) { $flag_in_define = 1; $name = $1; next MAIN_LOOP; } if (m/^\.\./) { $flag_in_define = 0; next MAIN_LOOP; } if ($flag_in_define) { chomp; $defines{$name} .= $_; next MAIN_LOOP; } } close FH; print Dumper(\%defines);

        And here is what it looks like when I run it agianst your sample file.

        C:\test>junk5 junk $VAR1 = { 'get_input' => ' select key, key1, key2, + passkey3, passkey4, print_name into input_booking_seq, f +ax_header, file_no, input_print_rates, myNoteId, print +_name from ed_table where ed_table.tag = \'BOOK\' + and ed_table.key = \'PRINT\' and ed_table.user_id = + user', 'get_user_info' => ' select user_loc,user_name, user +_company, into user_location, user_name, user_company, + from sec_header where user_id = lower(substr(user,5, +10))', 'loktabs' => ' loc_table book_tab, book_hazmat, custd +ata2, edit_table, book_rates, print_table in share update mo +de' };

        Download it, play with it, try and understand it. Please don't come back and ask for more until you do.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.