Need help from Perl expert

Discussion in 'Perl Misc' started by t53864@yahoo.com, Oct 14, 2005.

  1. Guest

    I am new to Perl and currently trying to debug a Perl script.
    I encountered a line which I have no clue what it is trying to do.
    Hope some experts here can help me out.

    grep { ${ $_ }[0] =~ /^$ptype/ } @ports

    Thanks in advance,

    TL
     
    , Oct 14, 2005
    #1
    1. Advertising

  2. Brian Wakem Guest

    wrote:

    > I am new to Perl and currently trying to debug a Perl script.
    > I encountered a line which I have no clue what it is trying to do.
    > Hope some experts here can help me out.
    >
    > grep { ${ $_ }[0] =~ /^$ptype/ } @ports
    >
    > Thanks in advance,
    >
    > TL



    I don't qualify as an expert, but perhaps this will shed some light.


    #!/usr/bin/perl

    use warnings;
    use strict;
    use Data::Dumper;

    my @ports = (
    [ 'Southampton', 'Hampshire' ],
    [ 'Portsmouth', 'Hampshire' ],
    [ 'Dover', 'Kent' ],
    );

    my $ptype = 'S|P';

    @ports = grep { ${ $_ }[0] =~ /^$ptype/ } @ports;

    print Dumper \@ports;


    -----------------------------


    $ perl tmp56.pl
    $VAR1 = [
    [
    'Southampton',
    'Hampshire'
    ],
    [
    'Portsmouth',
    'Hampshire'
    ]
    ];



    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
     
    Brian Wakem, Oct 14, 2005
    #2
    1. Advertising

  3. Paul Lalli Guest

    wrote:
    > I am new to Perl and currently trying to debug a Perl script.
    > I encountered a line which I have no clue what it is trying to do.
    > Hope some experts here can help me out.
    >
    > grep { ${ $_ }[0] =~ /^$ptype/ } @ports


    You don't need an expert. You just need a clearer understanding of the
    pieces of the language involved:

    grep { EXPR } LIST
    returns all those elements of LIST (@ports, in this case) for which
    EXPR is true, setting each element of LIST to $_ in EXPR

    EXPR =~ /^$ptype/
    returns true if the string contained in EXPR contains the substring
    "beginning of string, followed by the contents of $ptype"

    ${ REF }[0]
    is the first element of the array referenced by REF

    $_
    as we already said, will be aliased to each element of @ports

    Putting it all together:

    Loop through each member of @ports. For each member, look at the first
    element of the array that member references. If that element contains
    the variable $ptype at it's start, add this member of @ports to the
    list of values returned by the entire expression.

    For more information:
    perldoc -f grep
    perldoc perlref
    perldoc perlre

    Hope this helps,
    Paul Lalli
     
    Paul Lalli, Oct 14, 2005
    #3
    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. shapper
    Replies:
    7
    Views:
    315
    shapper
    Feb 27, 2007
  2. need expert help

    , Feb 23, 2005, in forum: C Programming
    Replies:
    0
    Views:
    354
  3. Y2J
    Replies:
    5
    Views:
    339
    Victor Bazarov
    Aug 19, 2006
  4. Replies:
    10
    Views:
    629
    Jonathan N. Little
    Oct 20, 2007
  5. Pseudonyme
    Replies:
    4
    Views:
    206
    J├╝rgen Exner
    Mar 24, 2010
Loading...

Share This Page