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

 my $string = 'London,Paris,Madrid,Berlin';

Above string is passed to a function defined in another script where I am storing this in an array.

 my @capitals= split(/,/,$string);

Sometime $string can contain only one element as London

I have a logic which requires me to check

If my @capitals array contains only 'London'

OR

it contains all elements of my @capitals.

Please help !!!

Replies are listed 'Best First'.
Re: Perl Array - What an array contains
by AnomalousMonk (Archbishop) on Aug 25, 2016 at 13:11 UTC
    If my @capitals array contains only 'London' ...

    An array contains only 'London' if it has only one element and that element is the string 'London':

    c:\@Work\Perl\monks>perl -wMstrict -le "my $string = 'London'; my @capitals = split /,/, $string; if (@capitals == 1 && $capitals[0] eq 'London') { print q{contains only 'London'} } " contains only 'London'

    ... OR it contains all elements of my @capitals.

    I don't understand what this requirement means. Can you please elaborate?


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

Re: Perl Array - What an array contains
by Eily (Monsignor) on Aug 25, 2016 at 12:02 UTC

    I'm not sure I understand your problem very well, in your first sentence you talk about a string but show something that looks like an array... See How do I post a question effectively?

    Anyway, to check if a string is included in another, the easiest way is to use the index function. Or to check if a string is present in an array, use grep.

Re: Perl Array - What an array contains
by Marshall (Canon) on Aug 25, 2016 at 18:41 UTC
    Your question is confusing. I suspect that you want to know how many "capitals" you have in the string? The scalar value of an array is the number of elements that it contains.
    #!/usr/bin/perl use strict; use warnings; my @strings = ('London, Paris','','London'); foreach my $string (@strings) { my @capitals= split(/,/,$string); print "list of capitals: @capitals\n"; print "number of capitals:".@capitals."\n"; print "number of capitals:",scalar @capitals,"\n"; print "lots of capitals\n\n" if (@capitals>1); print "one or no capitals\n\n" if (@capitals <= 1); } __END__ list of capitals: London Paris number of capitals:2 number of capitals:2 lots of capitals list of capitals: number of capitals:0 number of capitals:0 one or no capitals list of capitals: London number of capitals:1 number of capitals:1 one or no capitals
Re: Perl Array - What an array contains
by hippo (Archbishop) on Aug 25, 2016 at 12:47 UTC

    Just test $#capitals

    say $#capitals ? 'Lots of capitals' : "Just one: @capitals";

      Watch out, though. $#capitals would evaluate to true (-1) if the list has no capitals:

      perl -lE "@capitals=(); $,=', '; print scalar(@capitals), $#capitals, +$#capitals ? 'Lots' : 'One'; 0, -1, Lots perl -lE "@capitals=qw(London); $,=', '; print scalar(@capitals), $#ca +pitals, $#capitals ? 'Lots' : 'One'; 1, 0, One perl -lE "@capitals=qw(London Paris); $,=', '; print scalar(@capitals) +, $#capitals, $#capitals ? 'Lots' : 'One'; 2, 1, Lots

        There's yet another pitfall associated with the use of  $#array to determine the number of elements in an array. It's possible to assign to  $[ (see perlvar) to change the base index of Perl arrays. Doing so will result in your source entry appendage(s) receiving a sharp whack, so Don't Do That!SM

        Of course, it's possible to use  $#array for this purpose in a base-agnostic way:

        c:\@Work\Perl\monks>perl -wMstrict -le "print qq{perl array base is $[}; ;; my @ra = (); print qq{A: array (@ra) is empty} if $#ra + 1 - $[ == 0; ;; @ra = (1); print qq{B: array (@ra) is empty} if $#ra + 1 - $[ == 0; " perl array base is 0 A: array () is empty c:\@Work\Perl\monks>perl -wMstrict -le "$[ = 1; print qq{perl array base is $[}; ;; my @ra = (); print qq{A: array (@ra) is empty} if $#ra + 1 - $[ == 0; ;; @ra = (1); print qq{B: array (@ra) is empty} if $#ra + 1 - $[ == 0; " Use of assignment to $[ is deprecated at -e line 1. perl array base is 1 A: array () is empty

        But an array  @array evaluated in scalar context always yields the number of array elements, so I don't understand why one would ever do this in any other way.

        BTW: It's possible to assign a number other than 0 or 1 to $[, but then you're really on thin ice, so Double-Plus-Don't Do That!


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

        Indeed, but the possibility of "no capitals" was not in the spec. :-)