No, smart match operator cannot do this. I thought, maybe, you could use the return value, but no, you can't, the return value of smart match operator is boolean (true/false).

My test code

$ perl -E " my( $r, $a, @bba ) = ( 0, 1, 2..4,1,2..4 ); $r = $a ~~ @b +ba; say $r; " 1

You could accomplish this task by way of overload

#!/usr/bin/perl -- use 5.010; use strict; use warnings; my @array = ( 2..4,6,2..4 ); say "@array"; my $r = 6 ~~ bless\@array,'smart::ix'; say "$r $$r = $array[$$r]"; BEGIN { package smart::ix; use overload '~~' => \&rix; sub rix { my( $self, $val, $selfOnRight ) = @_; my $ix = 0; while( $ix <= @$self ){ return \$ix if $val ~~ $self->[$ix]; $ix++; } return ; } $INC{'smart/ix.pm'} = __FILE__; 1; } __END__ 2 3 4 6 2 3 4 SCALAR(0x9b386c) 3 = 6
This could make a good feature, say use feature 'smarter'; makes ~~ return an object, and sets the global variable $s (for smart) to the same value (like sort does for $a and $b), and ->ix returns the index (for this use case), or ->matches for the general case ....

Hmm, now that I think about, after consulting http://search.cpan.org/~jesse/perl-5.14.1/pod/perlsyn.pod#Smart_matching_in_detail some more, ~~ seems rather limited , oh well


In reply to Re: Smart match operator question by Anonymous Monk
in thread Smart match operator question by marcheenek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.