Professional Web Applications Themes

How to slice a split directly? - PERL Beginners

This works and does what I want it to: perl -e 'x = split("\.", "a.b.c"); print $x[0];' Why does not this work? perl -e 'print {split("\.", "a.b.c")}[0];' Is there a compact way to take a slice of a split (or other function that returns an array) without creating a temporary variable? Thanks, Siegfried...

  1. #1

    Default How to slice a split directly?

    This works and does what I want it to:

    perl -e 'x = split("\\.", "a.b.c"); print $x[0];'

    Why does not this work?
    perl -e 'print {split("\\.", "a.b.c")}[0];'

    Is there a compact way to take a slice of a split (or other function that
    returns an array) without creating a temporary variable?

    Thanks,
    Siegfried

    Siegfried Guest

  2. #2

    Default Re: How to slice a split directly?

    On Thu, 23 Sep 2004 13:43:08 -0600, Siegfried Heintze
    <com> wrote: 

    Because split doesn't return an array reference, it returns a list.

    print( (split(/\./, "a.b.c"))[0] );
     



    --
    -will
    http://www.wgunther.tk
    (the above message is double rot13 encoded for security reasons)

    Most Useful Perl Modules
    -strict
    -warnings
    -Devel::DProf
    -Benchmark
    -B::Dep
    -Data::Dumper
    -Clone
    -Perl::Tidy
    -Beautifier
    -DBD::SQLite
    William Guest

  3. #3

    Default Re: How to slice a split directly?

    From: "Siegfried Heintze" <com> 

    perl -e 'print ((split("\\.", "a.b.c"))[0]);'

    It's a bit tricky. The outermost braces belong to print(), the next
    ones enclose the call to split() so that it can be sliced and the
    innermost enclose the parameters for split(). Only the innermost may
    be left out.

    This makes the slicing of function result a bit clearer I think:

    ($hour, $minute, $sec) = (localtime())[2,1,0];

    Jenda
    ===== cz === http://Jenda.Krynicky.cz =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery

    Jenda Guest

  4. #4

    Default Re: How to slice a split directly?

    > This works and does what I want it to: 

    Sometimes when working out this kind of detail it is helpful to make a
    full script and activate strict/warnings. In the above case you get the
    following,
     
    Use of implicit split to _ is deprecated at -e line 1.
    Can't use string ("3") as an ARRAY ref while "strict refs" in use at -e
    line 1.

    Essentially C<split> returns a list, the construct C<{ }> is a way to
    slice into a hash, which you don't have. So you need to slice into a
    list, which in this case is done like,

    perl -Mstrict -w -e 'print ((split("\\.", "a.b.c"))[0]);'

    Notice the extra set of parens, otherwise you get a syntax error because
    C<print> would otherwise use the first set as an argument list.

    HTH,

    http://danconia.org
    Wiggins Guest

  5. #5

    Default Calling Perl From Java?

    I did a google search on calling Perl from Java. I found one site
    (http://ebb.org/perljvm/) that suggested the most promising project tackling
    this problem had been abandoned.

    The traditional approach is apparently very bersome: Have Java call C and
    C all Perl. This sounds very tedious.

    Does this sum up the situation?

    Thanks,
    Siegfried

    Siegfried Guest

  6. #6

    Default Re: How to slice a split directly?

    From: "Wiggins d Anconia" <org> 
    >
    > Sometimes when working out this kind of detail it is helpful to make a
    > full script and activate strict/warnings. In the above case you get
    > the following,

    > Use of implicit split to _ is deprecated at -e line 1.
    > Can't use string ("3") as an ARRAY ref while "strict refs" in use at
    > -e line 1.
    >
    > Essentially C<split> returns a list, the construct C<{ }> is a way to
    > slice into a hash, which you don't have.[/ref]

    There'd have to be a name of a variable between the and the opening
    curly brace for that to be a hash slice. This way its an array
    dereference:

    arr = (1,2,3);
    $rArr = \arr;
    other = {$rArr};

    Of course in this case you do not need the braces.

    You'd use the {} if the thing you need to dereference is more
    complex, for example if you need to dereference a function result.

    So
    print join(', ', {function('returning', 'arrayref')});
    would be correct, just like
    print join(', ', {function('returning', 'arrayref')}[1,2,4,7]);

    If you'd want just one item from the referenced array you would of
    course use ${}[] instead:

    print ${function('returning', 'arrayref')}[2];

    In this case the function doesn't return a reference, but a list so
    there is no point in trying to dereference anything :-)

    This is where the "Can't use string ("3") as an ARRAY ref while
    "strict refs" in use at -e line 1." message comes from. The split()
    is evaluated in scalar context and thus returns the number of
    elements found in the string. And then the code tries to do this:

    perl -e 'print {3}[0];'

    Jenda
    ===== cz === http://Jenda.Krynicky.cz =====
    When it comes to wine, women and song, wizards are allowed
    to get drunk and croon as much as they like.
    -- Terry Pratchett in Sourcery

    Jenda Guest

Similar Threads

  1. Defining a new slice
    By Andrew L. Gould in forum FreeBSD
    Replies: 5
    Last Post: February 17th, 01:16 PM
  2. Slice on slice
    By tevaji webforumsuser@macromedia.com in forum Macromedia Fireworks
    Replies: 2
    Last Post: August 2nd, 08:35 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139