Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Change variable multiple times within a single string?

by hdb (Monsignor)
on Apr 27, 2013 at 14:01 UTC ( [id://1030966]=note: print w/replies, xml ) Need Help??


in reply to Change variable multiple times within a single string?

Use a translation table (hash):

use strict; use warnings; my $string="A111B111A111B111"; my $yes_or_no="no"; my %decision = ( A => 'yes', B=> 'no' ); for (split //, $string ) { $yes_or_no = $decision{$_} if exists $decision{$_}; print "$_ ($yes_or_no)\n"; }

Replies are listed 'Best First'.
Re^2: Change variable multiple times within a single string?
by 2teez (Vicar) on Apr 27, 2013 at 15:35 UTC

    Hi hdb,
    Your solution would not only either set/reset and print the value 'yes' and 'no' for 'A' and 'B', but would also do for subsequent '1' also. And such that either 'yes' or 'no', were printed when the value of '1' is seen.
    Though, the OP wanted 'yes' or 'no', for 'A' and 'B'.
    So, I added this next if /1/; like so:

    for (split //, $string ) { next if /1/; $yes_or_no = $decision{$_} if exists $decision{$_}; print "$_ ($yes_or_no)\n"; }
    Though the OP didn't give any information on what he expect when '1' is seen.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      That is not correct! The value of the variable $yes_or_no will only be changed on the occurrence of 'A' or 'B' in the string. That is what  if exists $decision{$_}; is for, there is no entry of for '1' in the hash.

      Printing the variable in each line is only showing the current value. The OP had not really said what he wanted to do while running through the string, so I made something up.

      In a way, the print statement is completely superfluous, as the script would set the variable according to the wishes of the OP, but it would not be observable...

        ~ Printing the variable in each line is only showing the current value..

        That is excatly my point. Since, what to print when '1' is seen is not known, I think is better to leave it out.
        Your output:

        A (yes) 1 (yes) #? 1 (yes) #? 1 (yes) #? B (no) 1 (no) #? 1 (no) #? 1 (no) #? A (yes) 1 (yes) #? 1 (yes) #? 1 (yes) #? B (no) 1 (no) #? 1 (no) #? 1 (no) #?
        Output with a little modification:
        A (yes) B (no) A (yes) B (no)
        OR one can provide for '1' in hash like so:
        my %decision = ( A => 'yes', B=> 'no', '1'=>'', );
        Then the output shows:
        A (yes) 1 () 1 () 1 () B (no) 1 () 1 () 1 () A (yes) 1 () 1 () 1 () B (no) 1 () 1 () 1 ()
        Just saying.....

        If you tell me, I'll forget.
        If you show me, I'll remember.
        if you involve me, I'll understand.
        --- Author unknown to me

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1030966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 07:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found