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

Re: Loop Array - If $var is something write values until $var is something else

by AnomalousMonk (Archbishop)
on Sep 29, 2018 at 19:32 UTC ( [id://1223289]=note: print w/replies, xml ) Need Help??


in reply to Loop Array - If $var is something write values until $var is something else

maikelnight:   Further to Marshall's post:   Please see Short, Self-Contained, Correct Example. The monks grow sullen and silent when you make them do a lot of work in order to give free advice.

Others have noted that you don't seem to have warnings and strictures enabled. Let me repeat: Especially if you're a beginning Perler, always have the two statements
    use warnings;
    use strict;
at the beginning of your script. See (and use!) warnings and strict.

Finally, you seem confused about just what is in the  @cleared array; we certainly are. Using some kind of data dumper can bring much enlightenment. Popular are Data::Dumper::Dump() (which is core and so should already be loaded in your system), and Data::Dump::dd() (which I like better, but it's not core, so you may have to load it yourself). Use one or the other
    use Data::Dumper;
    ...
    print Dumper \@cleared;
or
    use Data::Dump;
    ...
    dd \@cleared;
and you may find yourself much further down the road to a working application!


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Loop Array - If $var is something write values until $var is something else
by maikelnight (Sexton) on Sep 29, 2018 at 20:05 UTC
    Sorry for confusing, should have removed the reference part as its not really important to my question. I removed it, use strict and warnings and now here is it:
    my @result; foreach my $x (@cleared) { if ($x =~ /JOB::(.*)/){ print $x, "\n"; } else { unless ($x =~ m/^Something/){ print "~~Something~~", $x, "\n"; } else { unless ($x =~ m/^Something Else/){ print "~~Something Else~~", $x, "\n"; } } } }
    Lets see the @cleared (Dump):
    $VAR1 = 'JOB::HEREISASTRING'; $VAR2 = 'Something'; $VAR3 = 'StringA'; $VAR4 = 'StringB'; $VAR5 = 'StringC'; $VAR6 = 'StringD'; $VAR7 = 'Something Else'; $VAR8 = 'StringE'; $VAR9 = 'StringF'; $VAR10 = 'StringG'; $VAR11 = 'StringH '; $VAR12 = 'JOB::HEREISANOTHERSTRING'; $VAR13 = 'Something'; $VAR14 = 'StringI'; $VAR15 = 'StringJ'; $VAR16 = 'StringK'; $VAR17 = 'StringL'; $VAR18 = 'Something Else'; $VAR19 = 'StringM'; $VAR20 = 'StringN'; $VAR21 = 'StringO'; $VAR22 = 'StringP ';
    Output:
    JOB::HEREISASTRING ~~Something Else~~Something ~~Something~~StringA ~~Something~~StringB ~~Something~~StringC ~~Something~~StringD ~~Something~~Something Else ~~Something~~StringE ~~Something~~StringF ~~Something~~StringG ~~Something~~StringH JOB::HEREISANOTHERSTRING ~~Something Else~~Something ~~Something~~StringI ~~Something~~StringJ ~~Something~~StringK ~~Something~~StringL ~~Something~~Something Else ~~Something~~StringM ~~Something~~StringN ~~Something~~StringO ~~Something~~StringP
    I wish output:
    JOB::HEREISASTRING ~~Something Else~~Something Else (could be removed) ~~Something~~StringA ~~Something~~StringB ~~Something~~StringC ~~Something~~StringD ~~Something Else~~Something Else (could be removed) ~~Something Else~~StringE ~~Something Else~~StringF ~~Something Else~~StringG ~~Something Else~~StringH JOB::HEREISANOTHERSTRING ~~Something Else~~Something Else (could be removed) ~~Something~~StringI ~~Something~~StringJ ~~Something~~StringK ~~Something~~StringL ~~Something Else~~Something Else (could be removed) ~~Something Else~~StringM ~~Something Else~~StringN ~~Something Else~~StringO ~~Something Else~~StringP
    In fact i want to tag my values from a start until a match occurs and tag it different and so on.... What comes to my mind is that with foreach loop variable $x changes and in the loop is again "Something"!?
      You seem to want the flip-flop operator. Ref: Range Operators in perlop. A "state variable" is not needed because the state is stored in the operator.
      ?type 1223291.pl use strict; use warnings; my @cleared = ( 'JOB::HEREISASTRING', 'Something', 'StringA', 'StringB', 'StringC', 'StringD', 'Something Else', 'StringE', 'StringF', 'StringG', 'StringH ', 'JOB::HEREISANOTHERSTRING', 'Something', 'StringI', 'StringJ', 'StringK', 'StringL', 'Something Else', 'StringM', 'StringN', 'StringO', 'StringP ', ); foreach (@cleared) { if (/JOB::(.*)/){ print $_, "\n"; next; } print '~~Something'; if ( !(/Something$/ ... /Something Else$/) or /Else/) { print ' Else'; } print "~~$_\n"; } ?perl 1223291.pl JOB::HEREISASTRING ~~Something~~Something ~~Something~~StringA ~~Something~~StringB ~~Something~~StringC ~~Something~~StringD ~~Something Else~~Something Else ~~Something Else~~StringE ~~Something Else~~StringF ~~Something Else~~StringG ~~Something Else~~StringH JOB::HEREISANOTHERSTRING ~~Something~~Something ~~Something~~StringI ~~Something~~StringJ ~~Something~~StringK ~~Something~~StringL ~~Something Else~~Something Else ~~Something Else~~StringM ~~Something Else~~StringN ~~Something Else~~StringO ~~Something Else~~StringP ?

      UPDATE: Added reference

      Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-03-29 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found