in reply to get a certain number of words from a line

$_=$l="how now brown cow hay is for horses oats are for goats mary lit +tle lamb"; #maybe a little inefficient, but no temp variables, all one line ($f,$b)=(join('',(split)[0..10]), join('',(split)[11..15])); #is there a better way to compose functions? golfers? ($f,$b)=map {join('',(split(/\s+/,$l))[@$_])}([0..10],[11..15]);
Update: Changed the 2nd snippet above so it doesn't depend on an obscure bug in 5.8.0. For the record you should use array references instead of list refs.

Replies are listed 'Best First'.
Re^2: get a certain number of words from a line
by Roy Johnson (Monsignor) on Jun 15, 2004 at 18:10 UTC
    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.
      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.