http://qs1969.pair.com?node_id=241089

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

Dear monks, Maybe somebody knows how do write a following code without a temporary variable @tmp:
my @tmp = split(/\//, '/home/bin/scripts/test.pl'); my $fname = pop @tmp;
This code doesn't work
my $fname = pop split(/\//, '/home/bin/scripts/test.pl');
because split in the scalar context returns a size of array. Of course, I can use regular expressions, I know it.
*-*-*-*-*-* { firstname => 'Michael', quote => 'Mice were crying and stinging but went on nibbling the + cactus', }

Replies are listed 'Best First'.
Re: Using split
by robartes (Priest) on Mar 07, 2003 at 09:03 UTC
    pop expects an array, whereas split returns a list. This is one of those cases where the difference between arrays (which can have elements popped off) and lists (which can't) is important. You could use some syntactic sugar to transform the list into an anonymous array, as in:
    $ perl -e 'print pop @{[split m|/|, "/home/bin/scripts/test.pl"]};' test.pl $

    Update: Changed example to OP's example.

    CU
    Robartes-

Re: Using split
by Enlil (Parson) on Mar 07, 2003 at 09:04 UTC
    try this:
    my $fname = (split/\//, '/home/bin/scripts/test.pl')[-1];

    update:After reading robartes answer, and rereading the question, I am guessing his answer is more along the lines of what you were looking for rather than how to get the last element of a list returned by a split statement.

    -enlil

Re: Using split
by BrowserUk (Patriarch) on Mar 07, 2003 at 09:29 UTC

    Seems a shame to generate a list and then discard most of it:).

    $_='/home/bin/scripts/test.pl'; my $file=substr $_, 1+rindex($_,'/');

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
Re: Using split
by tachyon (Chancellor) on Mar 07, 2003 at 12:10 UTC

    This is a very common Perl idiom that lets you toss stuff caught in regex matches into lists. Note you need list context so the () around $filename are required for $1 to be assigned to it correctly.

    ($filename) = '/path/to/file.name' =~ m!([^/]+)$/;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Using split
by diotalevi (Canon) on Mar 07, 2003 at 11:16 UTC

    Don't use split. Try this instead.

    '/home/bin/scripts/test.pl' =~ m{([^/]+)$} or die "Didn't match"; my $fname = $1;

    If you really wanted to use split then you could write this as my $fname = (split '/', '/home/bin/scripts/test.pl)[-1];


    Seeking Green geeks in Minnesota

Re: Using split
by Tomte (Priest) on Mar 07, 2003 at 09:24 UTC

    And if I'm not completly mistaken in what your snippet does and you're lucky enough to have perl 5.8 lying around, using File::Basename may be of interest.

    regards,
    tomte


    Edit:: You live and learn. I searched with perldoc -f basename and perldoc -q basename and then CPAN and since perl* both found nothing and CPAN returned perl-5.8.0 I started to non-think...

Re: Using split
by nite_man (Deacon) on Mar 07, 2003 at 12:32 UTC
    Dear monks,
    Thank you very much for your answers.
    As I see, I was near a descision.
    { firstname => 'Michael', quote => 'Mice were crying and stinging but went on nibbling the + cactus', }