in reply to I am now slice-aware

Slices do rock and so does "Effective Perl Programming", what confuses me is why is the code:

(@entry[0..8]) = split(/:/,$_); $newentry = join(":", @entry[0..8]);
Do you need the brackets around the @entry? What am I missing?

Why isn't it:

@entry = split /:/; # Automagically uses $_. $newentry = join (":", @entry[0..8]);
or on a dark day:$newentry = join(':',(split/:/)[0..8]);
What am I missing? Please help.

--
Brother Frankus. updated to remove stupid assumption about arrays.

Replies are listed 'Best First'.
Re: Re: I am now slice-aware
by davorg (Chancellor) on Apr 17, 2001 at 19:00 UTC

    You're missing the fact that split /:/ throws away any empty elements at the end of the string.

    Try this:

    $_ = '1:2:3:::::'; my @a = split /:/; print scalar @a; # prints 3
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      Just GREAT davorg!, now I have to go and amend my existing code. Wadda way to starta da week, if you want to snigger at my clumsier way:
      @foo=split/:/;<br> for ($#foo..$required_amt){push @foo, undef}
      Thanks to birdbrane for sharing :0).

      --
      
      Brother Frankus.
Re: Re: I am now slice-aware
by birdbrane (Chaplain) on Apr 17, 2001 at 19:19 UTC
    Hmmm, probably because I didn't think about doing it that way. I just assumed that I had to define the 9 elements (yes, in the /etc/shadow file, there are always 9 elements) before I could join them. Of course,

    $newentry = join(':',(split/:/)[0..8]);

    is much cooler. Thanks for the input.

    Of course, davorg is right, I forgot that split has a third field that I could have done the same thing. Oh well.

    Not feeling as enlightened, mostly feeling like a knucklehead now. :))

    Thus I am birdbrane

    update: Actually, I have just included the relevant lines of code, there is an "if" statement between the "split" and "join". Otherwise, I would be splitting and rejoining, while doing nothing in between. Doing something for no reason, hmmm, maybe that should be my philosophy for life... :)