Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #5095 > unrolled thread

Need help bringing select array lines together

Started byPaul <tester.paul@gmail.com>
First post2011-05-26 09:26 -0700
Last post2011-05-26 23:53 -0500
Articles 12 — 5 participants

Back to article view | Back to comp.lang.ruby


Contents

  Need help bringing select array lines together Paul <tester.paul@gmail.com> - 2011-05-26 09:26 -0700
    Re: Need help bringing select array lines together Johnny Morrice <spoon@killersmurf.com> - 2011-05-26 12:43 -0500
      Re: Need help bringing select array lines together Johnny Morrice <spoon@killersmurf.com> - 2011-05-26 12:54 -0500
      Re: Need help bringing select array lines together Paul <tester.paul@gmail.com> - 2011-05-26 14:24 -0700
        Re: Need help bringing select array lines together Johnny Morrice <spoon@killersmurf.com> - 2011-05-26 16:46 -0500
        Re: Need help bringing select array lines together 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-26 17:14 -0500
          Re: Need help bringing select array lines together Paul <tester.paul@gmail.com> - 2011-05-26 21:37 -0700
        Re: Need help bringing select array lines together 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-26 17:31 -0500
      Re: Need help bringing select array lines together 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-26 17:33 -0500
        Re: Need help bringing select array lines together Gary Wright <gwtmp01@mac.com> - 2011-05-26 17:45 -0500
          Re: Need help bringing select array lines together Johnny Morrice <spoon@killersmurf.com> - 2011-05-26 18:05 -0500
    Re: Need help bringing select array lines together Bala TS <bdeveloper01@yahoo.com> - 2011-05-26 23:53 -0500

#5095 — Need help bringing select array lines together

FromPaul <tester.paul@gmail.com>
Date2011-05-26 09:26 -0700
SubjectNeed help bringing select array lines together
Message-ID<f293c173-40a7-4d9d-b62b-9c00f079ac55@bl1g2000vbb.googlegroups.com>
Hi there, I am looking at some old, confusing ruby code that works but
is really ugly. I'm hoping someone here can help me find a more ruby
way of rewriting it. Rather than post the ugly code, I'll describe
what it is trying to do.

The code reads in each line of an array, looks for a closing/ending "
and puts multiple lines into one element of another array if they are
part of the same string.

input: data_array = [ " \"Only one line.\"", " \"line 1 ", " line 2.\"
" ]

so data_array.size = 3
----
> puts data_array
 "Only one line."
 "line 1
 line 2."
----

I need to bring linked lines together like this:
new_array = [ " \"Only one line.\"", " \"line 1 line 2.\" "]

=> new_array.size = 2

ideas?

TIA.

[toc] | [next] | [standalone]


#5097

FromJohnny Morrice <spoon@killersmurf.com>
Date2011-05-26 12:43 -0500
Message-ID<20110526184256.54f1bcf6@killersmurf.com>
In reply to#5095
> The code reads in each line of an array, looks for a closing/ending "
> and puts multiple lines into one element of another array if they are
> part of the same string.

Do you need to handle nested strings?

Here is a crude solution that doesn't.  I tried it on 1.9.2

# The messy input
mess = [ " \"Only one line.\"", " \"line 1 ", " line 2.\"" ]

# A single string, from the mess concatenated
single = mess.join ""
# Find all matches of the regex
matches = single.scan /("[^"]*")/
# Flatten those results,
# to get lines
lines = matches.flatten

# Pretty print the lines
p lines

I don't think any regex, being a finite automaton, could handle nested
strings properly.

Have you considered writing a small lexer and parser?  You may be
better doing that, can handle weirder strings too.

Cheers,
Johnny

[toc] | [prev] | [next] | [standalone]


#5099

FromJohnny Morrice <spoon@killersmurf.com>
Date2011-05-26 12:54 -0500
Message-ID<20110526185411.74f109b5@killersmurf.com>
In reply to#5097
> I don't think any regex, being a finite automaton, could handle nested
> strings properly.

Ignore this bit, it's false.

[toc] | [prev] | [next] | [standalone]


#5105

FromPaul <tester.paul@gmail.com>
Date2011-05-26 14:24 -0700
Message-ID<325f5d61-b988-47e4-8765-6aefdeebe6c9@p23g2000vbl.googlegroups.com>
In reply to#5097
Hi Johnny, thanks for the reply

On May 26, 1:43 pm, Johnny Morrice wrote:
>
> Do you need to handle nested strings?

I'm not sure. I don't think so. I think the content is scrubbed
beforehand to replace all embedded " with '.

>
> # The messy input
> mess = [ " \"Only one line.\"", " \"line 1 ", " line 2.\"" ]
>
> # A single string, from the mess concatenated
> single = mess.join ""
> matches = single.scan /("[^"]*")/
> lines = matches.flatten
>
> # Pretty print the lines
> p lines
>

I'm not sure about the 'join' solution for 2 reasons:
 1) this array may be 1000's of lines long and I wonder what the
performance will be like.
 2) I'm thinking about getting rid of the initial array and reading
the data straight from the input files. In that case, i would have to
read in each line anyway.

Having said that, I will give this a try and see how it works with the
test data I have. Thanks!

>
> Have you considered writing a small lexer and parser?  You may be
> better doing that, can handle weirder strings too.
>

No I haven't -- I don't know how to do that. yet.


Cheers!  Paul.

[toc] | [prev] | [next] | [standalone]


#5108

FromJohnny Morrice <spoon@killersmurf.com>
Date2011-05-26 16:46 -0500
Message-ID<20110526224603.7defa04d@killersmurf.com>
In reply to#5105
> > Have you considered writing a small lexer and parser?  You may be
> > better doing that, can handle weirder strings too.
> >
> 
> No I haven't -- I don't know how to do that. yet.

Hey I was brainfarting at that point, you really don't need to do that!
I brainfart a lot.  Should really think for a bit before sending emails
off...

[toc] | [prev] | [next] | [standalone]


#5109

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-26 17:14 -0500
Message-ID<15f0dba725735b0085f56c96bb926c5a@ruby-forum.com>
In reply to#5105
Paul wrote in post #1001335:
> I'm not sure about the 'join' solution for 2 reasons:

Okay.  How about:

original_lines = [
  %q{"Only one line."},
  %q{"line 1 },
  %q{ line 2."},
]

combined_lines = []
temp = ''

original_lines.each do |line|
  temp << line

  if temp[-1] == %q{"}
    combined_lines << temp
    temp = ''
  end
end

p combined_lines

--output:--
["\"Only one line.\"", "\"line 1  line 2.\""]

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5131

FromPaul <tester.paul@gmail.com>
Date2011-05-26 21:37 -0700
Message-ID<196bf131-5708-4105-a778-6087eb121c0a@g28g2000yqa.googlegroups.com>
In reply to#5109
On May 26, 6:14 pm, 7stud wrote:
>
> combined_lines = []
> temp = ''
>
> original_lines.each do |line|
>   temp << line
>
>   if temp[-1] == %q{"}
>     combined_lines << temp
>     temp = ''
>   end
> end
>
> p combined_lines
>
> --output:--
> ["\"Only one line.\"", "\"line 1  line 2.\""]
>

That code _almost_ works. I changed the one line to "temp[-1,1]" and
then it worked! =)

This looks exactly like what I need.  Thanks!

Cheers!  Paul.

[toc] | [prev] | [next] | [standalone]


#5110

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-26 17:31 -0500
Message-ID<5a14aeeff415a03ff1fe30fbe31114ee@ruby-forum.com>
In reply to#5105
Paul wrote in post #1001335:
>  2) I'm thinking about getting rid of the initial array and reading
> the data straight from the input files.

Okay.  How about:


require 'stringio'

str =<<'ENDOFSTRING'
"Only one line."
"line 1
line 2
line 3"
ENDOFSTRING

file = StringIO.new(str)
$/ = %Q{"\n}  #change input line separator


file.each do |line|
  line.chomp!("\n")
  line.gsub!("\n", ' ')
  p line
end

--output:--
"\"Only one line.\""
"\"line 1 line 2 line 3\""

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5111

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-05-26 17:33 -0500
Message-ID<be910e8888396ae75362ecb47c8bf0d5@ruby-forum.com>
In reply to#5097
Johnny M. wrote in post #1001292:
>
> I don't think any regex, being a finite automaton, could handle nested
> strings properly.
>

There are regexes for nested parentheses--they use recursive regexes.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#5112

FromGary Wright <gwtmp01@mac.com>
Date2011-05-26 17:45 -0500
Message-ID<05492149-5F61-4174-83A1-278C1BD7EC2C@mac.com>
In reply to#5111
On May 26, 2011, at 6:33 PM, 7stud -- wrote:

> Johnny M. wrote in post #1001292:
>> 
>> I don't think any regex, being a finite automaton, could handle nested
>> strings properly.
>> 
> 
> There are regexes for nested parentheses--they use recursive regexes.

You are both right.  Johnny is using the formal language definition of
regular expression while 7stud is using the term to refer to particular
programming language constructs.  Most modern regexp libraries allow
for patterns that go well beyond the formal language concept of
regular expressions.

Still, I'm not sure how you would defined nested strings using a single
quoting character: 

"stuff"otherstuff"morestuff"

Is that an example of a nested string or just two strings with otherstuff
inbetween?


Gary Wright

[toc] | [prev] | [next] | [standalone]


#5114

FromJohnny Morrice <spoon@killersmurf.com>
Date2011-05-26 18:05 -0500
Message-ID<20110527000539.2c1ba525@killersmurf.com>
In reply to#5112
> "stuff"otherstuff"morestuff"
> 
> Is that an example of a nested string or just two strings with
> otherstuff inbetween?

That's ambiguous.  Oh noes!

I am a sort of disorganised person.  I was thinking of matching
parenthesis, which you can't do with "regular" regular expressions.

Really I just made a stupid mistake!
I was thinking of nested stings like so "\"\\\"\\\"\"".

All you would have to look for is \", since \\\" ends with \".  So that
can be done without variable amounts of memory.

Hence, I sort of just blabbed without engaging the brain properly,
leading to this discussion.  Oh noes!

I'm not really sure why my mind drips out my ears, but hey I have some
cheese and wrote some ruby so it's all good.

Johnny

[toc] | [prev] | [next] | [standalone]


#5133

FromBala TS <bdeveloper01@yahoo.com>
Date2011-05-26 23:53 -0500
Message-ID<51ffeed7a8250b9d2ebc7a00605e8591@ruby-forum.com>
In reply to#5095
I think this may be solution about that problem

irb(main):027:0> a=" "
=> " "
irb(main):028:0> x=["line 1","line2","line3"]
=> ["line 1", "line2", "line3"]
irb(main):029:0> x.each do |r|
irb(main):030:1*  r=a+r
irb(main):031:1*  a=r+" "
irb(main):032:1> end
=> ["line 1", "line2", "line3"]
irb(main):033:0> puts a
 line 1 line2 line3
=> nil
irb(main):034:0>


by
bdeveloper01

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web