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

I have been playing with @+ and @- (see perldoc perlvar). Something that bugs me is why I can't do:

print "@-";

or

print "@+";

Or rather I can, but I just get @- or @+, which is not particularly helpful :-). Of course I don't need to use the print "@array" construct, I just wonder if there's a particular reason why it doesn't work here, while it works with Perl's other special array variables (@_ and @ARGV).

AIMS? (Am I Missing Something?)

dave

Replies are listed 'Best First'.
Re: Small question: Why can't I print "@-"?
by BrowserUk (Patriarch) on Sep 23, 2003 at 19:08 UTC

    You can interpolate both arrays, but you have to do so at the right time

    print "@-:@+" while 'the quick brown fox jumps over the lazy dog' =~ /(\b.+?\b)(.+ +?)\b/g 0 0 3:4 3 4 4 4 9:10 9 10 10 10 15:16 15 16 16 16 19:20 19 20 20 20 25:26 25 26 26 26 30:31 30 31 31 31 34:35 34 35 35 35 39:40 39 40 print "@-:@+" :

    Both arrays are reset before a new match is attempted, so when the match fails, they contain nothing to interpolate.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

      Right, thanks, this must be a version thing. If I use your code:

      print "@-:@+\n" while 'the quick brown fox jumps over the lazy dog' =~ /(\b.+?\b)(.+ +?)\b/g

      I get the following output:

      @-:@+ @-:@+ @-:@+ @-:@+ @-:@+ @-:@+ @-:@+ @-:@+
      whereas if I adapt it as per sauoq's suggestion below:

      print "@{-}:@{+}\n" while 'the quick brown fox jumps over the lazy dog' =~ /(\b.+?\b)(.+ +?)\b/g

      I get what I want.

      I use AS 5.61 on XP

      dave

        Yep, this a known bug in 5.6.1, but it is fixed in 5.8.0.

        -- Mike

        --
        XML::Simpler does not require XML::Parser or a SAX parser. It does require File::Slurp.
        -- grantm, perldoc XML::Simpler

        Ah! I ran the sample on AS 5.8.0


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
        If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Small question: Why can't I print "@-"?
by sauoq (Abbot) on Sep 23, 2003 at 19:09 UTC

    That's just the way string interpolation works. You can do it like this:

    print "@{+}";
    instead.

    -sauoq
    "My two cents aren't worth a dime.";
    
      # or print "@{[@+]}\n"; # or print join($"||"",@+),"\n";

      :-)


      ---
      demerphq

        First they ignore you, then they laugh at you, then they fight you, then you win.
        -- Gandhi


        Sure, you could use one of those. But, why would you?

        Would it improve efficiency? Uh uh. Clarity? Certainly not.

        I can't think of a good reason... can you?

        BTW, I didn't realize this was a "bug" fixed in 5.8. I was thinking that perl didn't like to interpolate any variables that didn't start with a \w character. But, putting the name in braces will work with other (admittedly poor choices for) variable names such as @% and @!, not just ones perl knows.

        -sauoq
        "My two cents aren't worth a dime.";
        
Re: Small question: Why can't I print "@-"?
by bart (Canon) on Sep 23, 2003 at 19:48 UTC
    A simple solution: set $, to something not empty. Then you can just drop the quotes.
    $_ = 'There is food at the bar'; /(fo+)/; $, = "-"; $\ = "\n"; print @-;
Re: Small question: Why can't I print "@-"?
by converter (Priest) on Sep 23, 2003 at 19:33 UTC

    If your code executes print "@-"; and the output is @-, then your perl is probaby version 5.005_03 or older. @- and @+ aren't supported until perl version 5.6, if I remember correctly.

    converter

    Updated: Interpolation of @- and @+ seems to be broken until version 5.008.

      No, I still see "@-", perl version = 5.6.1.

        You're right, my mistake. I tested with perl 5.8 and 5.00503, but not 5.6 or 5.6.1. Evidently 5.8 recognizes @- as an array without disambiguation via @{-} where versions up to at least 5.6.1 do not.

        converter