split function

Discussion in 'Perl Misc' started by learnperlscripting@gmail.com, Mar 22, 2007.

  1. Guest

    I am having a hard time understanding this perl scripting code:

    while (<STDIN>) {
    chomp;
    ($user, $gcos) = (split /:/) [0, 4];
    ($real) = split(/,/, $gcos);
    print "$user is $real \n";
    }

    i do not understand line 3 and 4. could someone please help me out.
    much appreciated.
     
    , Mar 22, 2007
    #1
    1. Advertising

  2. Ben Morrow Guest

    Quoth :
    > I am having a hard time understanding this perl scripting code:
    >
    > while (<STDIN>) {
    > chomp;
    > ($user, $gcos) = (split /:/) [0, 4];


    This line first calls the split function: perldoc -f split. It's in list
    context (we'll see why in a minute), and it defaults to splitting the
    string in $_ that we've just read from STDIN.

    The (...)[0,4] is a list slice (perldoc perldata, section "Slices"),
    which selects the first and fifth of the items returned by split.

    These two values are then assigned to the variables $user and $gecos
    respectively.

    > ($real) = split(/,/, $gcos);


    This is another split, this time specifying what string to split. The
    first result is assigned to the variable $real.

    > print "$user is $real \n";
    > }
    >
    > i do not understand line 3 and 4. could someone please help me out.
    > much appreciated.


    The program looks like a rather poor attempt at parsing a Unix passwd
    file. There are at least two bugs: the GECOS field should be split on
    the pattern /\s*,\s*/ instead, and the real name needs any instance of
    '&' replaced with an initial-caps version of $user. The program is not
    'strict'-safe: have you read the Posting Guidelines for this group yet?
    Also, if it is intended to parse the real /etc/passwd file, it would be
    much better to use the getpwent functions and the User::pwent module, so
    it works properly on systems that use other passwd facilities such as
    NIS.

    Ben

    --
    Raise your hand if you're invulnerable.
    []
     
    Ben Morrow, Mar 22, 2007
    #2
    1. Advertising

  3. Uri Guttman Guest

    >>>>> "l" == learnperlscripting <> writes:

    l> I am having a hard time understanding this perl scripting code:
    l> while (<STDIN>) {
    l> chomp;
    l> ($user, $gcos) = (split /:/) [0, 4];
    l> ($real) = split(/,/, $gcos);
    l> print "$user is $real \n";
    l> }

    l> i do not understand line 3 and 4. could someone please help me out.
    l> much appreciated.

    split is well documented. run this command:

    perldoc -f split

    why is your nick learnperlscripting? are you learning perl or offering
    to others to learn perl

    if you are learning perl you should first learn to look for help in
    perl's docs. all the functions and ops are fully documented there.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Mar 22, 2007
    #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. Replies:
    2
    Views:
    470
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    702
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    218
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    243
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    613
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page