Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, i wanna save the output in a variable for the below program. i am not able to do.Right now its just printing.pls do look in.
$PatternToMatch="^SELECT"; $tmpfile="pattern.txt"; open(MYFILE, "$tmpfile")|| die "Cannot create $tmpfile\n"; while (<MYFILE>) { if (/$PatternToMatch/) { print "$_"; } }
the input is,
SQL*Plus: Release 9.2.0.6.0 - Production on Wed Jan 21 11:09:19 2009 Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved. Connected to: Oracle9i Enterprise Edition Release 9.2.0.7.0 - 64bit Production With the Partitioning option JServer Release 9.2.0.7.0 - Production SQL> 2 'SELECTPOID_ID0FROM'||TABLE_NAME||'WHEREACCOUNT_OBJ_ID0=76362171;' ---------------------------------------------------------------------- +---------- SELECT poid_id0 FROM ACTIVE_SESSION_T WHERE account_obj_id0 = 7636217 +1; SELECT poid_id0 FROM ADMIN_ACTION_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AUDIT_ACCOUNT_PRODUCTS_T WHERE account_obj_id0 = + 76362171; SELECT poid_id0 FROM AU_BAL_GRP_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_DEAL_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_DEPENDENCY_T WHERE account_obj_id0 = 76362171 +; SELECT poid_id0 FROM AU_DEVICE_SERVICES_T WHERE account_obj_id0 = 763 +62171; SELECT poid_id0 FROM AU_DEVICE_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_DISCOUNT_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_GROUP_SHARING_MEMBERS_T WHERE account_obj_id0 + = 7636217 1; 'SELECTPOID_ID0FROM'||TABLE_NAME||'WHEREACCOUNT_OBJ_ID0=76362171;' ---------------------------------------------------------------------- +---------- SELECT poid_id0 FROM AU_GROUP_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_ORDERED_BALGROUP_T WHERE account_obj_id0 = 76 +362171; SELECT poid_id0 FROM AU_PAYINFO_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_PLAN_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_PRODUCT_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_RATE_PLAN_SELECTOR_T WHERE account_obj_id0 = +76362171; SELECT poid_id0 FROM AU_RATE_PLAN_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_RATE_T WHERE account_obj_id0 = 76362171;
the ouput obtained is,
SELECT poid_id0 FROM ACTIVE_SESSION_T WHERE account_obj_id0 = 7636217 +1; SELECT poid_id0 FROM ADMIN_ACTION_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AUDIT_ACCOUNT_PRODUCTS_T WHERE account_obj_id0 = + 76362171; SELECT poid_id0 FROM AU_BAL_GRP_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_DEAL_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_DEPENDENCY_T WHERE account_obj_id0 = 76362171 +; SELECT poid_id0 FROM AU_DEVICE_SERVICES_T WHERE account_obj_id0 = 763 +62171; SELECT poid_id0 FROM AU_DEVICE_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_DISCOUNT_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_GROUP_SHARING_MEMBERS_T WHERE account_obj_id0 + = 7636217 SELECT poid_id0 FROM AU_GROUP_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_ORDERED_BALGROUP_T WHERE account_obj_id0 = 76 +362171; SELECT poid_id0 FROM AU_PAYINFO_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_PLAN_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_PRODUCT_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_RATE_PLAN_SELECTOR_T WHERE account_obj_id0 = +76362171; SELECT poid_id0 FROM AU_RATE_PLAN_T WHERE account_obj_id0 = 76362171; SELECT poid_id0 FROM AU_RATE_T WHERE account_obj_id0 = 76362171;

Replies are listed 'Best First'.
Re: Output in variable
by jettero (Monsignor) on Jan 22, 2009 at 11:43 UTC
    You can open a filehandle to a scalar ref like so: my $output; open(my $fh, ">", \$output) or die $!. You would then print to the filehandle instead of STDOUT. I may not fully understand the question though.

    -Paul

      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
        hi... actually i wanna save the whole output in a variable. so that i can use it later.
        Tanx Loco... its working :)
      Right now i am printing the output directly by print "$_". but i wanna save this output in a variable. how can i do this ???
        Uhm, you already have the output in a variable. The variable is called $_. If you don't want to print it, then, well, perhaps you should consider not asking perl to print it.