Quantcast

Split string in nested list

classic Classic list List threaded Threaded
6 messages Options
shk
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Split string in nested list

shk
Hello,

I have list of next view:

[["admin@localhost"],[localhost@localhost],[user@localhost]]

How can i split by '@" this list?

I need following:

[["admin", "localhost"], ["localhost", "localhost"], [user, localhost]]

Thank you.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Split string in nested list

Jesper Louis Andersen-2
On Fri, Feb 11, 2011 at 17:50, shk <[hidden email]> wrote:

> I have list of next view:
>
> [["admin@localhost"],[localhost@localhost],[user@localhost]]

I assume you mean that localhost@locahost is a string and not an atom
in the following:

5> [string:tokens(S, "@") || [S] <-
[["admin@localhost"],["localhost@localhost"],["user@localhost"]]].
[["admin","localhost"],
 ["localhost","localhost"],
 ["user","localhost"]]
6>

But you may want to pull out the underlying strings out of the lists
first depending on what you are trying to do. In the above we use a
list comprehension under the *assumption* that its elements are of the
form [S] for some S which is a string. It will fail silently if they
are not and skip the element. Also string:tokens/2 is under the
assumption there are no double occurrence of an @ character in the
string.


--
J.

________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:[hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Split string in nested list

James Aimonetti-2
In reply to this post by shk
On 02/11/2011 08:50 AM, shk wrote:

> Hello,
>
> I have list of next view:
>
> [["admin@localhost"],[localhost@localhost],[user@localhost]]
>
> How can i split by '@" this list?
>
> I need following:
>
> [["admin", "localhost"], ["localhost", "localhost"], [user, localhost]]
>
> Thank you.
Is the list ["admin@localhost", "localhost@localhost", "user@localhost"]
or the one you wrote? They are different; yours is a list of lists, each
of which is a list with one element (the string), while mine is a list
of strings.

Either way, lists:map/2 should suffice to help convert the list; you
will need to unpack your inner lists within the map fun to get your
actual string.

--
James Aimonetti
Distributed Systems Engineer / DJ MC_

2600hz | http://2600hz.com
sip:[hidden email]
tel: 415.886.7905


________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:[hidden email]

shk
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Split string in nested list

shk
In reply to this post by Jesper Louis Andersen-2
Hello Jesper Louis Andersen. Thank you very much for your answer, you
very helped me.

________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:[hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Split string in nested list

Weerasak Chongnguluam
In reply to this post by shk
Hi,

You can use lists:map with string:tokens, like this.
lists:map(fun(URL) ->
    if is_atom(URL) ->
        string:tokens(atom_to_list(URL),"@");
       true ->
        string:tokens(URL,"@")
    end
    end,[["admin@localhost"],[localhost@localhost],[user@localhost]]
    ).
   
On Fri, 2011-02-11 at 08:50 -0800, shk wrote:

> Hello,
>
> I have list of next view:
>
> [["admin@localhost"],[localhost@localhost],[user@localhost]]
>
> How can i split by '@" this list?
>
> I need following:
>
> [["admin", "localhost"], ["localhost", "localhost"], [user, localhost]]
>
> Thank you.



________________________________________________________________
erlang-questions (at) erlang.org mailing list.
See http://www.erlang.org/faq.html
To unsubscribe; mailto:[hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Split string in nested list

Weerasak Chongnguluam
Sorry I'm incorrect argument pattern.

lists:map(fun([URL]) ->
   if is_atom(URL) ->
       string:tokens(atom_to_list(
>
> URL),"@");
>       true ->
>        string:tokens(URL,"@")
>    end
>    end,[["admin@localhost"],[localhost@localhost],[user@localhost]]
>    ).


2011/2/12 Weerasak Chongnguluam <[hidden email]>

> Hi,
>
> You can use lists:map with string:tokens, like this.
> lists:map(fun(URL) ->
>    if is_atom(URL) ->
>        string:tokens(atom_to_list(URL),"@");
>       true ->
>        string:tokens(URL,"@")
>    end
>    end,[["admin@localhost"],[localhost@localhost],[user@localhost]]
>    ).
>
> On Fri, 2011-02-11 at 08:50 -0800, shk wrote:
> > Hello,
> >
> > I have list of next view:
> >
> > [["admin@localhost"],[localhost@localhost],[user@localhost]]
> >
> > How can i split by '@" this list?
> >
> > I need following:
> >
> > [["admin", "localhost"], ["localhost", "localhost"], [user, localhost]]
> >
> > Thank you.
>
>
>
Loading...