my $short = (split /\//, $Server->File())[$#]

Discussion in 'Perl Misc' started by cate, Jan 10, 2010.

  1. cate

    cate Guest

    Is it possible to do something like that? I can make it work with in
    index number. I can make it work with the last array index.

    my $short = (split /\//, $Server->File())[$#]

    Thank you.
     
    cate, Jan 10, 2010
    #1
    1. Advertising

  2. cate wrote:
    > Is it possible to do something like that? I can make it work with in
    > index number.


    Ah, so then what is the problem/question? Why not use the solution that
    works if it works?

    my $short = (split /\//, $Server->File())[-1]

    > I can make it work with the last array index.
    >
    > my $short = (split /\//, $Server->File())[$#]


    You can? That certainly surprises me.

    Xho
     
    Xho Jingleheimerschmidt, Jan 10, 2010
    #2
    1. Advertising

  3. cate

    cate Guest

    Re: my $short = (split /\//, $Server->File())[$#]

    On Jan 10, 12:12 pm, Xho Jingleheimerschmidt <>
    wrote:
    > cate wrote:
    > > Is it possible to do something like that?  I can make it work with in
    > > index number.

    >
    > Ah, so then what is the problem/question?  Why not use the solution that
    > works if it works?
    >
    > my $short =  (split /\//, $Server->File())[-1]
    >
    > > I can make it work with the last array index.

    >
    > >    my $short =  (split /\//, $Server->File())[$#]

    >
    > You can?  That certainly surprises me.
    >
    > Xho


    thanks.... typo... only in the borros world thanks again
     
    cate, Jan 10, 2010
    #3
  4. cate

    Dr.Ruud Guest

    cate wrote:

    > my $short = (split /\//, $Server->File())[$#]


    my ($short) = $Server->File() =~ m~(?:.*/)?(.*)~;

    my ($short) = $Server->File() =~ m~([^/]*)$~;

    File::Basename

    --
    Ruud
     
    Dr.Ruud, Jan 10, 2010
    #4
  5. cate wrote:

    > Is it possible to do something like that? I can make it work with in
    > index number. I can make it work with the last array index.
    >
    > my $short = (split /\//, $Server->File())[$#]


    Sure you can do it, but not with $Server->File(), since it's not an
    array. Of course, you can build an array by using / as a delimiter and
    then split, but that would be doing double the work when you can
    determine the last element by split in the first place, or by using a
    regex instead.

    --
    Not really a wanna-be, but I don't know everything.
     
    Wanna-Be Sys Admin, Jan 11, 2010
    #5
  6. cate

    Guest

    On Sun, 10 Jan 2010 10:04:01 -0800 (PST), cate <> wrote:

    >Is it possible to do something like that? I can make it work with in
    >index number. I can make it work with the last array index.
    >
    > my $short = (split /\//, $Server->File())[$#]
    >
    >Thank you.


    Not really a good way to do it.
    Untested.

    $short = (reverse split(/\//, $Server->File) )[0] || 'empty';

    or

    $short = (@_= split /\//, $Server->File )[$#_] || 'empty';

    -sln
     
    , Jan 11, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    475
  2. David Geering

    longs, long longs, short short long ints . . . huh?!

    David Geering, Jan 8, 2007, in forum: C Programming
    Replies:
    15
    Views:
    579
    Keith Thompson
    Jan 11, 2007
  3. Replies:
    4
    Views:
    845
    Kaz Kylheku
    Oct 17, 2006
  4. Ioannis Vranos

    unsigned short, short literals

    Ioannis Vranos, Mar 4, 2008, in forum: C Programming
    Replies:
    5
    Views:
    703
    Eric Sosman
    Mar 5, 2008
  5. Andre
    Replies:
    5
    Views:
    550
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page