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

I have a list formated as: user:pass I want to create my script that goes through the list and makes each side of the : an varible as in: user:pass = $var1:$var2 that way i can play with the variables.

Replies are listed 'Best First'.
Re: Parsing list
by vinoth.ree (Monsignor) on Mar 02, 2014 at 08:31 UTC

    In Perl a very useful function is split, which splits up a string and places it into an array.

    For Example,

    my $str = "user:group:root:testing"; @user = split(/:/, $string);

    Here @user holds the details of the $str.

    As in your case you have only two fields delimited by ':', you can directly split and save the values into the variables as below,

    my $yourstr = "user:pass"; my ($User,$Pass) = split (/:/,$yourstr); print "User:$User\n"; print "Pass:$Pass\n";

    All is well
      It also looks like a CSV format.
      csv
      J -
Re: Parsing list
by hdb (Monsignor) on Mar 02, 2014 at 07:39 UTC

    You want to look at split.

Re: Parsing list
by ww (Archbishop) on Mar 02, 2014 at 13:19 UTC

    I think I understand that you have some data (we'll discuss list later) comprised of something like this:

    user:pass user1:pass1 use:pas ^ ^

    What is the separator (ie, the character my caret's point to)? A space, semi-colon, LF, null or something else (including no separator at all)?

    Now, as to "list" -- what's shown is a list. But is that what your source looks like (minus the assignment and the rest of the Perl-ish elements, or does your source look like this?

    user:pass user1:pass1 use:pas

    or, perhaps, like this (markup typo fixed here; thanks, kcott!)

    user:pass,user1:pass1,use:pas

    For the first, a simple split statement will do very nicely; the second begs that you read the data into an array and use for $_(@array){.... while the third looks like an invitation to use the appropriate csv module to put you data into a format you know how to mung.

    Update (illustration):

    #!/usr/bin/perl use 5.016; use warnings; # NOT JUST BTW: this will not work without modification for some possi +ble forms of data! my $list = "user:passer use:pass user1:pass1"; say $list; say "That was \$list; these are the elements of \@arr:"; my @arr= split / /, $list; for (@arr) { say $_; my $current_element = $_; my ($first, $second) = split /:/, $current_element; say "\$first: $first & \$second: $second"; }

    Output:

    user:passer use:pass user1:pass1 That was $list; these are the elements of @arr: user:passer $first: user & $second: passer use:pass $first: use & $second: pass user1:pass1 $first: user1 & $second: pass1
    Come, let us reason together: Spirit of the Monastery
Re: Parsing list
by LanX (Saint) on Mar 02, 2014 at 05:41 UTC
    normally I would ask "what did you try?" but in your case "what did you try to compose an understandable question?"

    How do I post a question effectively?

    Cheers Rolf

    ( addicted to the Perl Programming Language)