in reply to Re: Can't use string ("1") as a SCALAR ref while "strict refs" in use
in thread Can't use string ("1") as a SCALAR ref while "strict refs" in use

This is the part of my actual code:
.............. if($body_text=~m/Opening Balance (.*) Net Transaction (.*) Cash Total +(.*) Card Total (.*) Cheque Total (.*) Grand Total ([^ ]*) /) { $open_bal=join "",split ",",substr($1,1); $net_trx=join "",split ",",substr($2,1); $cash_total=join "",split ",",substr($3,1); $card_total=join "",split ",",substr($4,1); $cheque_total=join "",split ",",substr($5,1); $grand_total=join "",split ",",substr($6,1); print "Open Balance: ",$open_bal,"\n"; print "Net Transaction: ",$net_trx,"\n"; print "Cash Total: ",$cash_total,"\n"; print "Card Total: ",$card_total,"\n"; print "Cheque Total: ",$cheque_total,"\n"; print "Grand Total: ",$grand_total,"\n"; } .....................

I just wanted to shorten the code using this line instead
($open_bal,$net_trx,$cash_total,$card_total,$cheque_total,$grand_total +)=join "",split ",",substr($$_,1) for (1..6);

May be im doing something awkward
:)

Replies are listed 'Best First'.
Re^3: Can't use string ("1") as a SCALAR ref while "strict refs" in use
by ikegami (Patriarch) on Mar 09, 2010 at 15:29 UTC
    if ( my ($open_bal, $net_trx, $cash_total, $card_total, $cheque_total, $grand_total) = $body_text =~ / Opening[ ]Balance[ ](.*)[ ]Net[ ]Transaction[ ](.*)[ ] Cash[ ]Total[ ](.*)[ ]Card[ ]Total[ ](.*)[ ] Cheque[ ]Total[ ](.*)[ ]Grand[ ]Total[ ]([^ ]*) /x ) { tr/$,//d for $open_bal, $net_trx, $cash_total, $card_total, $cheque_total, $grand_total; print ... }
    Update: I hadn't escaped all the spaces. Fixed.
Re^3: Can't use string ("1") as a SCALAR ref while "strict refs" in use
by ikegami (Patriarch) on Mar 09, 2010 at 19:28 UTC

    This may also work:

    if ( my ($open_bal, $net_trx, $cash_total, $card_total, $cheque_total, $grand_total) = $body_text =~ /\$(\S+)/g ) { tr/,//d for $open_bal, $net_trx, $cash_total, $card_total, $cheque_total, $grand_total; print ... }

    With both this solution and the original solution, you can use apply to shorten the code.

    use List::MoreUtils qw( apply ); if ( my ($open_bal, $net_trx, $cash_total, $card_total, $cheque_total, $grand_total) = apply { tr/,//d; } $body_text =~ /\$(\S+)/g ) { print ... }
Re^3: Can't use string ("1") as a SCALAR ref while "strict refs" in use
by ssandv (Hermit) on Mar 09, 2010 at 19:13 UTC
    You most certainly are doing something not only awkward, but pointless. Matching in a list context returns the contents of the match variables as a list in numerical order. You should assign that to an array or list of scalars (ikegami's solution right here is a list of scalars). You gain absolutely nothing by delving into the bottomless pit of symbolic references in this case.