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

Hi,

here is my code

my @intf_list = ("xe-4/0/1", "xe-2/2/1"); foreach my $myint (@intf_list) { my @outputlist = split m([-/]), $myint; push (@fpc, $outputlist[1]); push (@pic, $outputlist[2]); push (@port, $outputlist[3]); print "my fpc of intf is $fpc[0]\n"; print "mu pic is $pic[0]\n"; print "my port is $port[0]\n";

Above execution results in my fpc of intf is 4 mu pic is 0 my port is 1 which is working fine as as per my expectation

Now, as per the following output. Adding the output in code section

show chassis fpc pic-status 4 Slot 4 Online MPC4E 3D 2CGE+8XGE PIC 0 Online 4x10GE SFPP PIC 1 Online 1X100GE CFP PIC 2 Online 4x10GE SFPP PIC 3 Online 1X100GE CFP

i want to find , how many PIC are there and there numbers say, PIC 0 , PIC 1 etc and find whether its online and also find whether it has only 10GE port. And compare, this PIC number with the above output $pic and do a decision. I tried the following code based on the output of the previous code section

$rh->cmd("show chassis fpc pic-status $fpc[0]"); my $output = $rh->get_response; my @outputlist = join s/(^\s+|\s+$)//g, $output;

I thought to elimate the space and combine all the test from the output, and then do a regexp to find the required parameters as mentioned above. But, i do not see any difference in the output, after the above code is executed. Is there a way to solve this?

Replies are listed 'Best First'.
Re: Regexp question on combining white spaces and comparisons
by AnomalousMonk (Archbishop) on May 16, 2015 at 22:02 UTC
    my @outputlist = join s/(^\s+|\s+$)//g, $output;

    I, too, am puzzled by what you are trying to achieve overall. But the particular statement quoted above is problematic. Because the prototype (see Prototypes in perlsub) of join is  $@

    c:\@Work\Perl>perl -e "print prototype 'CORE::join'" $@
    the  s/(^\s+|\s+$)//g expression (which does a substitution on the  $_ default scalar) is evaluated in scalar context and the success or failure of the substitution (1 or empty string, respectively) | number of substitutions is returned and used as the join string (update: i.e., as the result of the EXPR in join EXPR,LIST).
    c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my $output = 'something or other'; ;; $_ = ' who knows what '; my @outputlist = join s/(^\s+|\s+$)//g, $output; dd \@outputlist; " ["something or other"]
    But that doesn't matter because there is only one item in the join list, namely $output, so the result of the joining EXPR is never used.
    c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my $output = 'something or other'; ;; $_ = ' who knows what '; my @outputlist = join 'anything at all', $output; dd \@outputlist; " ["something or other"]

    Update: Here's the result of an experiment showing what happens if there is more than one string to be join-ed:

    c:\@Work\Perl>perl -wMstrict -MData::Dump -le "my $output = 'something or other'; ;; $_ = ' who knows what '; my @outputlist = join s/(^\s+|\s+$)//g, $output, 'hoo', 'ha'; dd \@outputlist; print qq{default scalar after s///: '$_'}; " ["something or other2hoo2ha"] default scalar after s///: 'who knows what'
    In any event, there will never be more than one element in the  @outputlist array.


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

Re: Regexp question on combining white spaces and comparisons
by ww (Archbishop) on May 16, 2015 at 22:30 UTC
    Ln8: print "my fpc of intf is $fpc[0]\n";

    If you have not declared @fpc outside the foreach (beginning at Ln3) it's going to change with each iteration because the array gets overwritten each time thru the loop; if you have declared it outside the loop it won't change.

    The absence of a my @fpc declaration in what you've shown us suggests either that you're not using strict (hint, hint -- usually a very good idea) or that you've declared the array in code you haven't shown. Neither of those possibilities makes it easy to be confident that any particular observation will help you.

    So, as others have advised (or hinted) above, you need to clarify your exposition... or your thinking.


    check Ln42!

Re: Regexp question on combining white spaces and comparisons
by Anonymous Monk on May 16, 2015 at 17:42 UTC
    I thought to ... do a regexp to find the required parameters as mentioned above. But, i do not see any difference in the output, after the above code is executed.

    You haven't shown us the code you attempted to use to parse the lines like "PIC 0  Online       4x10GE SFPP". Or are you asking us to write that for you?

    my @outputlist = join s/(^\s+|\s+$)//g, $output;

    This does not really make sense, or you're trying to do too much in one line - if you could explain what this line of code is supposed to be doing that might help.