scala filtering list of strings with multiple commas












1















I am very new to Scala and I am not even sure how to describe my question properly, but I will do my best to explain with examples.
I want to filter a list of strings with a condition but that list contains a lot of commas. The list contains userID, artID and ratings. eg)



val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")


With 3,123,5 as an example, 3 is the userID, 123 is the artID and 5 is the rating.
What I want to do is take the ratings that is above 4 and return a list of (userID, artID) only (return as List[(String, String)]). I've been trying various ways but kept failing and I have no idea how to begin now.










share|improve this question





























    1















    I am very new to Scala and I am not even sure how to describe my question properly, but I will do my best to explain with examples.
    I want to filter a list of strings with a condition but that list contains a lot of commas. The list contains userID, artID and ratings. eg)



    val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")


    With 3,123,5 as an example, 3 is the userID, 123 is the artID and 5 is the rating.
    What I want to do is take the ratings that is above 4 and return a list of (userID, artID) only (return as List[(String, String)]). I've been trying various ways but kept failing and I have no idea how to begin now.










    share|improve this question



























      1












      1








      1








      I am very new to Scala and I am not even sure how to describe my question properly, but I will do my best to explain with examples.
      I want to filter a list of strings with a condition but that list contains a lot of commas. The list contains userID, artID and ratings. eg)



      val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")


      With 3,123,5 as an example, 3 is the userID, 123 is the artID and 5 is the rating.
      What I want to do is take the ratings that is above 4 and return a list of (userID, artID) only (return as List[(String, String)]). I've been trying various ways but kept failing and I have no idea how to begin now.










      share|improve this question
















      I am very new to Scala and I am not even sure how to describe my question properly, but I will do my best to explain with examples.
      I want to filter a list of strings with a condition but that list contains a lot of commas. The list contains userID, artID and ratings. eg)



      val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")


      With 3,123,5 as an example, 3 is the userID, 123 is the artID and 5 is the rating.
      What I want to do is take the ratings that is above 4 and return a list of (userID, artID) only (return as List[(String, String)]). I've been trying various ways but kept failing and I have no idea how to begin now.







      string scala list split






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 '18 at 6:37









      Bogdan Vakulenko

      1,035215




      1,035215










      asked Nov 21 '18 at 5:08









      NoooooobieNoooooobie

      113




      113
























          4 Answers
          4






          active

          oldest

          votes


















          2














          You say you want to filter a "list of strings." So I take it your input might look like this.



          val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")


          That being the case there is the problem of making numeric comparisons of String elements.



          "4"  < "5"  //true
          "14" > "5" //false


          You can cast the String to an Int before comparison, but that should also include tests for casting failure.



          Here's a slightly different approach that simply passes all rating strings, numeric or otherwise, that evaluates as greater than "4". Strings that don't split() into the correct number of sub-strings are filtered out.



          lst.map(_.split(",")).collect{
          case Array(uID,aID,r) if r.length > 1 || r > "4" => (uID,aID)
          }
          //res0: List[(String,String)] = List((3, 123), (4, 1210))





          share|improve this answer

































            0














            Check this out:



            scala> val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")
            lst: List[String] = List(1,1,4, 1,1060,2, 3,123,5, 4,1210,5, 4,1213,4)

            scala> lst.filter(x=> { val y=x.split(",").map(_.toInt); y(2)>4}).map(x=>{val y = x.split(","); (y(0),y(1))} )
            res16: List[(String, String)] = List((3,123), (4,1210))

            scala>





            share|improve this answer































              0














              val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

              val res = lst
              .map(_.split(","))
              .collect {
              case Array(a,b,c) if c.toInt>4 => (a,b)
              }

              println(res)





              share|improve this answer


























              • I get an error at case List(a,b,c) if c>4 => (a,b) ^ <console>:22: error: type mismatch; is there any way to return it as List[(String, String)] ??

                – Noooooobie
                Nov 21 '18 at 6:13













              • do you have val lst = List(1,1,4, 1,1060,2, 3,123,5, 4,1210,5, 4,1213,4) or val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4") as an input? I mean lst var contains only array of values or list of strings like "3,123,5" ?

                – Bogdan Vakulenko
                Nov 21 '18 at 6:20











              • I am sorry I confused you, it's the latter one, val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                – Noooooobie
                Nov 21 '18 at 6:25











              • added to answer

                – Bogdan Vakulenko
                Nov 21 '18 at 6:26











              • It works very well. Thank you very much! I have one more question, is it not possible to use grouped(3) instead of sliding?

                – Noooooobie
                Nov 21 '18 at 6:41



















              0














              you can do that in various ways:



              val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

              val ans: List[(String, String)] = lst.filter(elem => elem.split(",")(2).toInt > 4).map { e =>
              val s = e.split(",")
              (s(0), s(1))
              }

              val ans2: List[(String, String)] = lst.map {
              _.split(",")
              } collect {
              case Array(a, b, c) if c.toInt > 4 => (a, b)
              }

              val ans3: List[(String, String)] = lst.foldLeft(List[(String, String)]()) {
              (a, b) => {
              val sp = b.split(",")
              if (sp(2).toInt > 4) {
              (sp(0), sp(1)) :: a
              } else a
              }
              }

              println(ans) //List((3,123), (4,1210))
              println(ans2) //List((3,123), (4,1210))
              println(ans3) //List((4,1210), (3,123))





              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405580%2fscala-filtering-list-of-strings-with-multiple-commas%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                2














                You say you want to filter a "list of strings." So I take it your input might look like this.



                val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")


                That being the case there is the problem of making numeric comparisons of String elements.



                "4"  < "5"  //true
                "14" > "5" //false


                You can cast the String to an Int before comparison, but that should also include tests for casting failure.



                Here's a slightly different approach that simply passes all rating strings, numeric or otherwise, that evaluates as greater than "4". Strings that don't split() into the correct number of sub-strings are filtered out.



                lst.map(_.split(",")).collect{
                case Array(uID,aID,r) if r.length > 1 || r > "4" => (uID,aID)
                }
                //res0: List[(String,String)] = List((3, 123), (4, 1210))





                share|improve this answer






























                  2














                  You say you want to filter a "list of strings." So I take it your input might look like this.



                  val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")


                  That being the case there is the problem of making numeric comparisons of String elements.



                  "4"  < "5"  //true
                  "14" > "5" //false


                  You can cast the String to an Int before comparison, but that should also include tests for casting failure.



                  Here's a slightly different approach that simply passes all rating strings, numeric or otherwise, that evaluates as greater than "4". Strings that don't split() into the correct number of sub-strings are filtered out.



                  lst.map(_.split(",")).collect{
                  case Array(uID,aID,r) if r.length > 1 || r > "4" => (uID,aID)
                  }
                  //res0: List[(String,String)] = List((3, 123), (4, 1210))





                  share|improve this answer




























                    2












                    2








                    2







                    You say you want to filter a "list of strings." So I take it your input might look like this.



                    val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")


                    That being the case there is the problem of making numeric comparisons of String elements.



                    "4"  < "5"  //true
                    "14" > "5" //false


                    You can cast the String to an Int before comparison, but that should also include tests for casting failure.



                    Here's a slightly different approach that simply passes all rating strings, numeric or otherwise, that evaluates as greater than "4". Strings that don't split() into the correct number of sub-strings are filtered out.



                    lst.map(_.split(",")).collect{
                    case Array(uID,aID,r) if r.length > 1 || r > "4" => (uID,aID)
                    }
                    //res0: List[(String,String)] = List((3, 123), (4, 1210))





                    share|improve this answer















                    You say you want to filter a "list of strings." So I take it your input might look like this.



                    val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")


                    That being the case there is the problem of making numeric comparisons of String elements.



                    "4"  < "5"  //true
                    "14" > "5" //false


                    You can cast the String to an Int before comparison, but that should also include tests for casting failure.



                    Here's a slightly different approach that simply passes all rating strings, numeric or otherwise, that evaluates as greater than "4". Strings that don't split() into the correct number of sub-strings are filtered out.



                    lst.map(_.split(",")).collect{
                    case Array(uID,aID,r) if r.length > 1 || r > "4" => (uID,aID)
                    }
                    //res0: List[(String,String)] = List((3, 123), (4, 1210))






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 21 '18 at 6:25

























                    answered Nov 21 '18 at 5:53









                    jwvhjwvh

                    27.9k52141




                    27.9k52141

























                        0














                        Check this out:



                        scala> val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")
                        lst: List[String] = List(1,1,4, 1,1060,2, 3,123,5, 4,1210,5, 4,1213,4)

                        scala> lst.filter(x=> { val y=x.split(",").map(_.toInt); y(2)>4}).map(x=>{val y = x.split(","); (y(0),y(1))} )
                        res16: List[(String, String)] = List((3,123), (4,1210))

                        scala>





                        share|improve this answer




























                          0














                          Check this out:



                          scala> val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")
                          lst: List[String] = List(1,1,4, 1,1060,2, 3,123,5, 4,1210,5, 4,1213,4)

                          scala> lst.filter(x=> { val y=x.split(",").map(_.toInt); y(2)>4}).map(x=>{val y = x.split(","); (y(0),y(1))} )
                          res16: List[(String, String)] = List((3,123), (4,1210))

                          scala>





                          share|improve this answer


























                            0












                            0








                            0







                            Check this out:



                            scala> val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")
                            lst: List[String] = List(1,1,4, 1,1060,2, 3,123,5, 4,1210,5, 4,1213,4)

                            scala> lst.filter(x=> { val y=x.split(",").map(_.toInt); y(2)>4}).map(x=>{val y = x.split(","); (y(0),y(1))} )
                            res16: List[(String, String)] = List((3,123), (4,1210))

                            scala>





                            share|improve this answer













                            Check this out:



                            scala> val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")
                            lst: List[String] = List(1,1,4, 1,1060,2, 3,123,5, 4,1210,5, 4,1213,4)

                            scala> lst.filter(x=> { val y=x.split(",").map(_.toInt); y(2)>4}).map(x=>{val y = x.split(","); (y(0),y(1))} )
                            res16: List[(String, String)] = List((3,123), (4,1210))

                            scala>






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 '18 at 6:40









                            stack0114106stack0114106

                            4,4002422




                            4,4002422























                                0














                                val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                                val res = lst
                                .map(_.split(","))
                                .collect {
                                case Array(a,b,c) if c.toInt>4 => (a,b)
                                }

                                println(res)





                                share|improve this answer


























                                • I get an error at case List(a,b,c) if c>4 => (a,b) ^ <console>:22: error: type mismatch; is there any way to return it as List[(String, String)] ??

                                  – Noooooobie
                                  Nov 21 '18 at 6:13













                                • do you have val lst = List(1,1,4, 1,1060,2, 3,123,5, 4,1210,5, 4,1213,4) or val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4") as an input? I mean lst var contains only array of values or list of strings like "3,123,5" ?

                                  – Bogdan Vakulenko
                                  Nov 21 '18 at 6:20











                                • I am sorry I confused you, it's the latter one, val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                                  – Noooooobie
                                  Nov 21 '18 at 6:25











                                • added to answer

                                  – Bogdan Vakulenko
                                  Nov 21 '18 at 6:26











                                • It works very well. Thank you very much! I have one more question, is it not possible to use grouped(3) instead of sliding?

                                  – Noooooobie
                                  Nov 21 '18 at 6:41
















                                0














                                val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                                val res = lst
                                .map(_.split(","))
                                .collect {
                                case Array(a,b,c) if c.toInt>4 => (a,b)
                                }

                                println(res)





                                share|improve this answer


























                                • I get an error at case List(a,b,c) if c>4 => (a,b) ^ <console>:22: error: type mismatch; is there any way to return it as List[(String, String)] ??

                                  – Noooooobie
                                  Nov 21 '18 at 6:13













                                • do you have val lst = List(1,1,4, 1,1060,2, 3,123,5, 4,1210,5, 4,1213,4) or val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4") as an input? I mean lst var contains only array of values or list of strings like "3,123,5" ?

                                  – Bogdan Vakulenko
                                  Nov 21 '18 at 6:20











                                • I am sorry I confused you, it's the latter one, val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                                  – Noooooobie
                                  Nov 21 '18 at 6:25











                                • added to answer

                                  – Bogdan Vakulenko
                                  Nov 21 '18 at 6:26











                                • It works very well. Thank you very much! I have one more question, is it not possible to use grouped(3) instead of sliding?

                                  – Noooooobie
                                  Nov 21 '18 at 6:41














                                0












                                0








                                0







                                val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                                val res = lst
                                .map(_.split(","))
                                .collect {
                                case Array(a,b,c) if c.toInt>4 => (a,b)
                                }

                                println(res)





                                share|improve this answer















                                val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                                val res = lst
                                .map(_.split(","))
                                .collect {
                                case Array(a,b,c) if c.toInt>4 => (a,b)
                                }

                                println(res)






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 21 '18 at 6:55

























                                answered Nov 21 '18 at 5:19









                                Bogdan VakulenkoBogdan Vakulenko

                                1,035215




                                1,035215













                                • I get an error at case List(a,b,c) if c>4 => (a,b) ^ <console>:22: error: type mismatch; is there any way to return it as List[(String, String)] ??

                                  – Noooooobie
                                  Nov 21 '18 at 6:13













                                • do you have val lst = List(1,1,4, 1,1060,2, 3,123,5, 4,1210,5, 4,1213,4) or val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4") as an input? I mean lst var contains only array of values or list of strings like "3,123,5" ?

                                  – Bogdan Vakulenko
                                  Nov 21 '18 at 6:20











                                • I am sorry I confused you, it's the latter one, val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                                  – Noooooobie
                                  Nov 21 '18 at 6:25











                                • added to answer

                                  – Bogdan Vakulenko
                                  Nov 21 '18 at 6:26











                                • It works very well. Thank you very much! I have one more question, is it not possible to use grouped(3) instead of sliding?

                                  – Noooooobie
                                  Nov 21 '18 at 6:41



















                                • I get an error at case List(a,b,c) if c>4 => (a,b) ^ <console>:22: error: type mismatch; is there any way to return it as List[(String, String)] ??

                                  – Noooooobie
                                  Nov 21 '18 at 6:13













                                • do you have val lst = List(1,1,4, 1,1060,2, 3,123,5, 4,1210,5, 4,1213,4) or val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4") as an input? I mean lst var contains only array of values or list of strings like "3,123,5" ?

                                  – Bogdan Vakulenko
                                  Nov 21 '18 at 6:20











                                • I am sorry I confused you, it's the latter one, val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                                  – Noooooobie
                                  Nov 21 '18 at 6:25











                                • added to answer

                                  – Bogdan Vakulenko
                                  Nov 21 '18 at 6:26











                                • It works very well. Thank you very much! I have one more question, is it not possible to use grouped(3) instead of sliding?

                                  – Noooooobie
                                  Nov 21 '18 at 6:41

















                                I get an error at case List(a,b,c) if c>4 => (a,b) ^ <console>:22: error: type mismatch; is there any way to return it as List[(String, String)] ??

                                – Noooooobie
                                Nov 21 '18 at 6:13







                                I get an error at case List(a,b,c) if c>4 => (a,b) ^ <console>:22: error: type mismatch; is there any way to return it as List[(String, String)] ??

                                – Noooooobie
                                Nov 21 '18 at 6:13















                                do you have val lst = List(1,1,4, 1,1060,2, 3,123,5, 4,1210,5, 4,1213,4) or val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4") as an input? I mean lst var contains only array of values or list of strings like "3,123,5" ?

                                – Bogdan Vakulenko
                                Nov 21 '18 at 6:20





                                do you have val lst = List(1,1,4, 1,1060,2, 3,123,5, 4,1210,5, 4,1213,4) or val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4") as an input? I mean lst var contains only array of values or list of strings like "3,123,5" ?

                                – Bogdan Vakulenko
                                Nov 21 '18 at 6:20













                                I am sorry I confused you, it's the latter one, val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                                – Noooooobie
                                Nov 21 '18 at 6:25





                                I am sorry I confused you, it's the latter one, val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                                – Noooooobie
                                Nov 21 '18 at 6:25













                                added to answer

                                – Bogdan Vakulenko
                                Nov 21 '18 at 6:26





                                added to answer

                                – Bogdan Vakulenko
                                Nov 21 '18 at 6:26













                                It works very well. Thank you very much! I have one more question, is it not possible to use grouped(3) instead of sliding?

                                – Noooooobie
                                Nov 21 '18 at 6:41





                                It works very well. Thank you very much! I have one more question, is it not possible to use grouped(3) instead of sliding?

                                – Noooooobie
                                Nov 21 '18 at 6:41











                                0














                                you can do that in various ways:



                                val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                                val ans: List[(String, String)] = lst.filter(elem => elem.split(",")(2).toInt > 4).map { e =>
                                val s = e.split(",")
                                (s(0), s(1))
                                }

                                val ans2: List[(String, String)] = lst.map {
                                _.split(",")
                                } collect {
                                case Array(a, b, c) if c.toInt > 4 => (a, b)
                                }

                                val ans3: List[(String, String)] = lst.foldLeft(List[(String, String)]()) {
                                (a, b) => {
                                val sp = b.split(",")
                                if (sp(2).toInt > 4) {
                                (sp(0), sp(1)) :: a
                                } else a
                                }
                                }

                                println(ans) //List((3,123), (4,1210))
                                println(ans2) //List((3,123), (4,1210))
                                println(ans3) //List((4,1210), (3,123))





                                share|improve this answer




























                                  0














                                  you can do that in various ways:



                                  val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                                  val ans: List[(String, String)] = lst.filter(elem => elem.split(",")(2).toInt > 4).map { e =>
                                  val s = e.split(",")
                                  (s(0), s(1))
                                  }

                                  val ans2: List[(String, String)] = lst.map {
                                  _.split(",")
                                  } collect {
                                  case Array(a, b, c) if c.toInt > 4 => (a, b)
                                  }

                                  val ans3: List[(String, String)] = lst.foldLeft(List[(String, String)]()) {
                                  (a, b) => {
                                  val sp = b.split(",")
                                  if (sp(2).toInt > 4) {
                                  (sp(0), sp(1)) :: a
                                  } else a
                                  }
                                  }

                                  println(ans) //List((3,123), (4,1210))
                                  println(ans2) //List((3,123), (4,1210))
                                  println(ans3) //List((4,1210), (3,123))





                                  share|improve this answer


























                                    0












                                    0








                                    0







                                    you can do that in various ways:



                                    val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                                    val ans: List[(String, String)] = lst.filter(elem => elem.split(",")(2).toInt > 4).map { e =>
                                    val s = e.split(",")
                                    (s(0), s(1))
                                    }

                                    val ans2: List[(String, String)] = lst.map {
                                    _.split(",")
                                    } collect {
                                    case Array(a, b, c) if c.toInt > 4 => (a, b)
                                    }

                                    val ans3: List[(String, String)] = lst.foldLeft(List[(String, String)]()) {
                                    (a, b) => {
                                    val sp = b.split(",")
                                    if (sp(2).toInt > 4) {
                                    (sp(0), sp(1)) :: a
                                    } else a
                                    }
                                    }

                                    println(ans) //List((3,123), (4,1210))
                                    println(ans2) //List((3,123), (4,1210))
                                    println(ans3) //List((4,1210), (3,123))





                                    share|improve this answer













                                    you can do that in various ways:



                                    val lst = List("1,1,4", "1,1060,2", "3,123,5", "4,1210,5", "4,1213,4")

                                    val ans: List[(String, String)] = lst.filter(elem => elem.split(",")(2).toInt > 4).map { e =>
                                    val s = e.split(",")
                                    (s(0), s(1))
                                    }

                                    val ans2: List[(String, String)] = lst.map {
                                    _.split(",")
                                    } collect {
                                    case Array(a, b, c) if c.toInt > 4 => (a, b)
                                    }

                                    val ans3: List[(String, String)] = lst.foldLeft(List[(String, String)]()) {
                                    (a, b) => {
                                    val sp = b.split(",")
                                    if (sp(2).toInt > 4) {
                                    (sp(0), sp(1)) :: a
                                    } else a
                                    }
                                    }

                                    println(ans) //List((3,123), (4,1210))
                                    println(ans2) //List((3,123), (4,1210))
                                    println(ans3) //List((4,1210), (3,123))






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Nov 21 '18 at 10:02









                                    Raman MishraRaman Mishra

                                    1,4431418




                                    1,4431418






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405580%2fscala-filtering-list-of-strings-with-multiple-commas%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        鏡平學校

                                        ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                                        Why https connections are so slow when debugging (stepping over) in Java?