in reply to Re: Output in variable
in thread Output in variable

Hi. Im not shure what you really want to load into the variable, select or all the line after the text select? Try to change $_ for $1 (this its how perl saves the matched regex after the next iteration or line) But...explain better what you want to get in the output

Replies are listed 'Best First'.
Re^3: Output in variable
by Anonymous Monk on Jan 22, 2009 at 11:51 UTC
    hi... actually i wanna save the whole output in a variable. so that i can use it later.
      maybe try this:
      
      $PatternToMatch="^SELECT";
      $tmpfile="pattern.txt";
      open(MYFILE, "$tmpfile")|| die "Cannot create $tmpfile\n";
      while (<MYFILE>) {
      if (/$PatternToMatch/)
      {
       push (@array, $_);
        
      }
      }
      
      
      As you are reading the file, you need to push every time perl founds Select to and array, because the variable get overwritten with every success match. After that, recover all the data from the array when you need.
      foreach $control (@array) {
         print "Line -> $control\n";
        }
      
        Tanx Sombrerero loco.... its working :)
      What, you mean like ...
      $PatternToMatch="^SELECT"; $tmpfile="pattern.txt"; $var = ''; open(MYFILE, "<$tmpfile")|| die "Cannot create $tmpfile\n"; while (<MYFILE>) { $var .= $_ if /$PatternToMatch/; } . . . . eval $var;
      ??

      A user level that continues to overstate my experience :-))
Re^3: Output in variable
by Anonymous Monk on Jan 22, 2009 at 12:04 UTC
    Tanx Loco... its working :)