in reply to Re: get a certain number of words from a line
in thread get a certain number of words from a line

You seem to be under the impression that
\(0..10)
is the same as
[0..10]
Update: It appears that they are the same for this particular case, which is (IMO) a Bad Idea, since the \() construct is already a not-uncommon source of confusion. Why was it considered important to make \(something) behave like [something] iff something is a range?

We're not really tightening our belts, it just feels that way because we're getting fatter.

Replies are listed 'Best First'.
Re^3: get a certain number of words from a line
by sleepingsquirrel (Chaplain) on Jun 15, 2004 at 18:26 UTC
    I'm under the impression that \(0..10) is a reference to an 11 item list (0,1,2,3,4,5,6,7,8,9,10). And further more, I'm under the impression that...
    $ref=\(1..10); @a[@$ref];
    ...is an eleven item array slide of @a, which happens to be equivalent to @a[0..10]. Try it, you might like it ;)
      I'd like to try it, but I don't have perl5 where I am this week. My understanding is that \(1..10) is shorthand for (\1,\2,\3,...,\10), and that your list will be flattened, so that (\(1..10),\(11..15)) is the same as \(1..15).

      It's not a feature I've used, myself, so I could certainly be misunderstanding it. If I am, I would genuinely appreciate someone making it clear to me.

      It took me a minute to decode "array slide" as "array slice". I thought you were talking about a feature I'd never heard of!

      Update: from perlref:

      Taking a reference to an enumerated list is not the same as using squa +re brackets--instead it's the same as creating a list of references!

      We're not really tightening our belts, it just feels that way because we're getting fatter.
        Hmmm... Apparently not for my perl...
        greg@sparky:~/test$ cat ./list_ref #!/usr/bin/perl -w @a=qw/zero one two three four five/; $ref=\(1..3); @b=@a[@$ref]; print "\n \@a=@a\n \$ref=$ref\n \@b=@b\n\n"; greg@sparky:~/test$ ./list_ref @a=zero one two three four five $ref=ARRAY(0x813dcc0) @b=one two three greg@sparky:~/test$ perl -v This is perl, v5.8.0 built for i486-linux Copyright 1987-2002, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. <snip>
        ...And sorry for the typo. This thread (among others) has finally inspired me to create my first perlmonks .sig.


        -- All code is 100% tested and functional unless otherwise noted.
        "Enumerated list" must be the key concept.
        greg@sparky:~/test$ cat enum #!/usr/bin/perl -w use Data::Dumper; $ref=\(0..3); $enum_list=\(4,5,6); print "\n".Dumper($ref)."\n"; print "\n".Dumper($enum_list)."\n"; greg@sparky:~/test$ ./enum $VAR1 = [ 0, 1, 2, 3 ]; $VAR1 = \6;


        -- All code is 100% tested and functional unless otherwise noted.