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

Hi maikelnight!,
First when you post a question here, the ideal situation is that you provide a completely runnable example including a sample of the data. That way I can just download the code into my Perl environment, push the "go button" and replicate exactly what you are seeing. update: Basically the easier you make it for the Monks to run your code, the more likely you are to get a prompt and good response. Right now I'd have to work my brain a bit to reverse engineer some data that would produce your results. Things are much better if I don't have to do that.

Do you have use strict; use warnings;? That is important. It looks like @cleared is an AoA (Array of Array), i.e. a 2-D structure. This code: ($x =~ m/^Something Else/) won't work because $x is a reference to an array. You need $x->[someIndex]to get a string to compare against. I am surprised if what you have doesn't throw some kind of an error? Why don't you rename $x, $row_ref or something like that? $x is not very descriptive.

This does look weird:

unless ($x =~ m/^Something Else/){ print "~~Something Else~~", $x, "\n"; }
That is the same as: print ~~Something Else~~ if $x is not Something Else , which is kinda weird.
if ($x->[someIndex] !~ m/^Something Else/){}
Does that help?

Enclose the output in code tags so that we can see the line breaks.

Replies are listed 'Best First'.
Re^2: Loop Array - If $var is something write values until $var is something else
by johngg (Canon) on Sep 29, 2018 at 18:05 UTC
    It looks like @cleared is an AoA

    The curly braces suggest to me that it might be an AoH instead.

    johngg@shiraz:~/perl/Monks$ perl -Mstrict -Mwarnings -E ' my @AoH = ( { jobName => q{Director}, jobProfile => q{Three day week}, }, { jobName => q{Manager}, jobProfile => q{9to5}, }, ); foreach my $job ( @AoH ) { say $job; say $job->{ jobProfile }; }' HASH(0x562033e731e0) Three day week HASH(0x562033e92488) 9to5

    I agree though that the unless ($x =~ m/^Something Else/){ does look weird as it would seem to be trying to match against a stringified reference. I suspect that we are not seeing the whole story.

    Cheers,

    JohnGG

      I agree. I misread the fonts between { and [ without my reading glasses! My dev environment has larger fonts than my browser!
      In any event, these 2 code statements from the OP are inconsistent with the type of value of $x:
      my $jobprofil = $x->{JobProfil}; ...later this... unless ($x =~ m/^Something Else/){
      Something is wrong here. I am hoping that a full runnable code example with use strict; use warnings; will be very helpful.
        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"!?