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

I don't see how this code works... but does code from a subroutine to get clearcase view set.

my @ccView = `cleartool pwv`; my $setView = (grep /Set view:/, @ccView)[0]; my $view = (split(:/ , $setView))[1];
I don't get the array element number of a scalar value in the statement starting "my $view..."

Replies are listed 'Best First'.
Re: split function
by hdb (Monsignor) on Apr 28, 2015 at 13:25 UTC

    Just a typo?

    my $view = (split(/:/ , $setView))[1];

    How about

    use strict; use warnings;

      Not sure what "typo" but here is the entire subroutine. The perl program uses strict and warnings. Code runs produces desired output, I just don't understand why. Not my code just trying to understand the reference to and array element when I don't see an array element available?

      sub get_cc_view{ my @ccView = (); @ccView = (grep /Set view:/, @ccView)[0]; my $view = (split(/:/ , $setView))[1]; $view =~ s/\s//g; $CC_View = $view; $CC_View = "View: $CC_View"; }

      I understand all code except my $view = (split(/:/ , $setView))1

        I understand all code except my $view = (split(/:/ , $setView))1

        split returns a list, and a list, properly parenthesized, can be subscripted:

        c:\@Work\Perl\monks>perl -wMstrict -le "my $one = ('zero', 'uno', 'two')[1]; print qq{'$one'}; " 'uno'


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

Re: split function
by toolic (Bishop) on Apr 28, 2015 at 13:45 UTC
    Post your actual code. That code does not compile. This does, with some phony input data. It should work, unless your input data is not what you think it is.
    use warnings; use strict; #my @ccView = `cleartool pwv`; my @ccView = map{"Set view: $_"}5..9; my $setView = (grep /Set view:/, @ccView)[0]; my $view = (split(/:/ , $setView))[1]; #my $view = (split(:/ , $setView))[1]; print "$view\n"; __END__ 5

    The Basic debugging checklist

      Thanx for the input. The original code compiles and produces expected output. I did not understand that split returns an array (aka a list).

Re: split function
by pme (Monsignor) on Apr 28, 2015 at 13:25 UTC
    I think slash is missing in front of ':'.
    my $view = (split(/:/ , $setView))[1];