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

I often find myself writing code like the following:
my @temp = split ",", $foo; print "$temp[2]\n";
And then I never use temp again. Is there anyway I can combine the above two lines into a single command? Thanks.

20030806 Edit by jeffa: Changed title from 'a quick question from a perl newbie '

Replies are listed 'Best First'.
•Re: Retrieve only what I need from split without using temp array
by merlyn (Sage) on Aug 05, 2003 at 21:34 UTC
Re: Retrieve only what I need from split without using temp array
by Limbic~Region (Chancellor) on Aug 05, 2003 at 21:37 UTC
    MasterGremlin,
    Someone will likely beat me to the punch, but what you want to do is treat the split anonymously. You can then get the index you want and throw away the rest.
    print ((split ',' , $foo)[2])); print "\n";

    That just looks nasty. I use this quite a bit when using stat and am only interested in say the file size, but then I am assigning it to a single scalar $size as merlyn shows, which eliminates the need for the second set of parens.

    Cheers - Limbic~Region
    Update Added parens to get it to work.

      um - why dont you use File::Stat's OO interface - surely it is also clearer to others what you are doing
      use File::Stat; print File::Stat->new($file)->size(), "\n";
      granted, this is a module that isn't standard in Perl, as opposed to stat(), so you need to do the MakeMaker waltz, but I think it is worth going the extra mile for the much clearer code.
      Is there a reason why you prefer stat() ?
        granted, this is a module that isn't standard in Perl, as opposed to stat(), so you need to do the MakeMaker waltz, but I think it is worth going the extra mile for the much clearer code.

        In many cases, scripts have to run on multiple platforms, with different versions of perl/mod_perl. In those situations, it's painful to have to build File::Stat just so someone doesn't have to see my $size=(stat $file)[7], which is pretty clear anyhow if you assign it to an appropriately named variable.

        If you KNOW all the platforms have 5.8.0 or better available, you could use File::stat, but I'm not sure that's that big a win either over just plain stat. I guess I just prefer the builtin in this case.
        --
        Mike

        Or even just: my $size = -s $file;

        bbfu
        Black flowers blossom
        Fearless on my breath

Re: Retrieve only what I need from split without using temp array
by broquaint (Abbot) on Aug 05, 2003 at 22:51 UTC
    Or yet another way to do it ...
    my $foo = 'foo,bar,baz'; print +(split ',' => $foo)[2], $/; __output__ baz
    The + force the evaluation on the right-hand side and then we just access the 3rd element from the list returned by split.
    HTH

    _________
    broquaint

Re: Retrieve only what I need from split without using temp array
by demerphq (Chancellor) on Aug 05, 2003 at 22:14 UTC

    Well, this is a little overkill for this situation, sometimes

    print do{my @temp=split /,/,$foo; $temp[2]},"\n";

    comes in useful:

    my $slurped=do{local $/; open my $fh,$file or die "$file:$!"; <$fh>};

    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...