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


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

Splitting each_cons?

Started bySimon Harrison <simon@simonharrison.net>
First post2011-04-03 13:45 -0500
Last post2011-04-03 16:51 -0500
Articles 13 — 6 participants

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


Contents

  Splitting each_cons? Simon Harrison <simon@simonharrison.net> - 2011-04-03 13:45 -0500
    Re: Splitting each_cons? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-03 13:51 -0500
    Re: Splitting each_cons? Simon Harrison <simon@simonharrison.net> - 2011-04-03 14:13 -0500
    Re: Splitting each_cons? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-03 15:24 -0500
      Re: Splitting each_cons? Brian Candler <b.candler@pobox.com> - 2011-04-03 15:28 -0500
    Re: Splitting each_cons? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-03 15:35 -0500
    Re: Splitting each_cons? Simon Harrison <simon@simonharrison.net> - 2011-04-03 16:07 -0500
      Re: Splitting each_cons? Josh Cheek <josh.cheek@gmail.com> - 2011-04-03 17:36 -0500
      Re: Splitting each_cons? Rob Biedenharn <Rob@AgileConsultingLLC.com> - 2011-04-04 07:42 -0500
    Re: Splitting each_cons? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-03 16:28 -0500
    Re: Splitting each_cons? Simon Harrison <simon@simonharrison.net> - 2011-04-03 16:33 -0500
    Re: Splitting each_cons? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-03 16:35 -0500
    Re: Splitting each_cons? Simon Harrison <simon@simonharrison.net> - 2011-04-03 16:51 -0500

#2202 — Splitting each_cons?

FromSimon Harrison <simon@simonharrison.net>
Date2011-04-03 13:45 -0500
SubjectSplitting each_cons?
Message-ID<5871e9ab38bdcb62e11f435fc17d510d@ruby-forum.com>
I'm not sure if each_cons can do what I'm trying to achieve:

one : vol1
one : vol2
three : vol3



irb(main):052:0> films
=> [["one", "vol1"], ["one", "vol2"], ["three", "vol3"]]

irb(main):053:0> films.each_cons(1) { |f| print f, " " }
onevol1 onevol2 threevol3 => nil

irb(main):054:0> films.each_cons(1) { |f| print f, " ", "\n" }
onevol1
onevol2
threevol3
=> nil

irb(main):055:0> films.each_cons(1) { |f| puts f }
one
vol1
one
vol2
three
vol3
=> nil


Thanks for any help.

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

[toc] | [next] | [standalone]


#2203

FromJesús Gabriel y Galán <jgabrielygalan@gmail.com>
Date2011-04-03 13:51 -0500
Message-ID<BANLkTinxmviTNe3XSgK-Qeqa8ESR58mjvQ@mail.gmail.com>
In reply to#2202
On Sun, Apr 3, 2011 at 8:45 PM, Simon Harrison <simon@simonharrison.net> wrote:
> I'm not sure if each_cons can do what I'm trying to achieve:
>
> one : vol1
> one : vol2
> three : vol3

To achieve this, you don't need each_cons, a simple each should suffice:

ruby-1.8.7-p334 :001 > films = [["one", "vol1"], ["one", "vol2"],
["three", "vol3"]]
 => [["one", "vol1"], ["one", "vol2"], ["three", "vol3"]]
ruby-1.8.7-p334 :002 > films.each {|film| puts "#{film.first} : #{film.last}"}
one : vol1
one : vol2
three : vol3

each_cons is typically used to traverse an array sliding several
elements at a time, for example:

ruby-1.8.7-p334 :003 > a = (1..20).to_a
 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
ruby-1.8.7-p334 :004 > a.each_cons(3) {|x,y,z| p [x,y,z]}
[1, 2, 3]
[2, 3, 4]
[3, 4, 5]
[4, 5, 6]
[5, 6, 7]
[6, 7, 8]
[7, 8, 9]
[8, 9, 10]
[9, 10, 11]
[10, 11, 12]
[11, 12, 13]
[12, 13, 14]
[13, 14, 15]
[14, 15, 16]
[15, 16, 17]
[16, 17, 18]
[17, 18, 19]
[18, 19, 20]

Jesus.

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


#2205

FromSimon Harrison <simon@simonharrison.net>
Date2011-04-03 14:13 -0500
Message-ID<b48ba6088a08709b10dff95c353ece5b@ruby-forum.com>
In reply to#2202
Jesus: you have helped me once again. With your (and others)
assistance, I've now completed my first program that actually does
something useful. For a while now I've been considering abandoning
learning programming as I find it hard going. Thankfully, people such as
yourself spare the time to answer beginners questions and get us going
in the right direction.

I'm truly grateful to you, and everyone else here, who shares knowledge
with those seeking help. God bless you.

Here is the program (I know it needs tidying up :)


=====================


#!/opt/jruby/bin/jruby

require 'csv'

def load_xvid_file(path_to_csv)
  @films = []
  csv_data = CSV.read(path_to_csv)
  csv_data.shift
  csv_data.each do |row|
    @films << [row[0], row[1]]
  end
end

def search_for_film
  print "Enter name of film to search for: "
  film = gets.chomp.downcase
  results = []
  @films.each { |f| results << f unless f.grep(/#{film}/i).empty? }
  if results.empty?
    puts "Nothing found."
    prompt
  else
    puts
    puts "Results"
    puts "======="
    puts
    results.each { |f|  puts "#{f.first} : #{f.last}" }
    puts
    prompt
  end
 end

def prompt
  print "Search again? (y or n) "
  answer = gets.chomp.downcase

  case answer
  when /^y/
    search_for_film
  when /^n/
    puts "Goodbye."
    exit
  else
    prompt
  end
end

load_xvid_file("/home/simon/Documents/CSV/XviD.csv")
search_for_film

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

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


#2211

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-03 15:24 -0500
Message-ID<233827b0b1dbc92b5beba2df1bfcdcc5@ruby-forum.com>
In reply to#2202
I would do it like this:


films = [["one", "vol1"], ["one", "vol2"], ["three", "vol3"]]

films.each do |arr|
  puts arr.join(' : ')
end

--output:--
one : vol1
one : vol2
three : vol3


And if you want to save the strings in a new_array--rather than print 
them--you can use map():

films = [["one", "vol1"], ["one", "vol2"], ["three", "vol3"]]

results = films.map do |arr|
  arr.join(' : ')
end

p results

--output:--
["one : vol1", "one : vol2", "three : vol3"]


Array#join() and String#split() should be in every beginner's arsenal. 
As for map(), it sends each element of the array to the block, and then 
shoves the return value of the block into a new array.  Here is a 
simpler example:

films = [["one", "vol1"], ["one", "vol2"], ["three", "vol3"]]

results = films.map do |arr|
  "hello"
end

p results

--output:--
["hello", "hello", "hello"]

For each element of the array, the block returns one value, which is 
stored in a new array.

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

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


#2212

FromBrian Candler <b.candler@pobox.com>
Date2011-04-03 15:28 -0500
Message-ID<5eedb149cc204076093042bfffd01d46@ruby-forum.com>
In reply to#2211
7stud -- wrote in post #990714:
> I would do it like this:
>
>
> films = [["one", "vol1"], ["one", "vol2"], ["three", "vol3"]]
>
> films.each do |arr|
>   puts arr.join(' : ')
> end
>
> --output:--
> one : vol1
> one : vol2
> three : vol3

Or: since the elements you're yielding are themselves also arrays, ruby 
can automatically assign them to individual variables if you wish (the 
so-called "auto-splat" feature)

films = [["one", "vol1"], ["one", "vol2"], ["three", "vol3"]]

films.each do |label, volume|
  puts "#{label}: #{volume}"
end

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

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


#2216

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-03 15:35 -0500
Message-ID<ed4c37140a178a0d5c1b885b64137021@ruby-forum.com>
In reply to#2202
>> For a while now I've been considering abandoning
>> learning programming as I find it hard going.

Programming is a required skill.  Stick with it.   Next, you might try 
reading the currently playing films off a website.  As a first step, go 
to www.google.com and grab the whole web page, and print out the first 
200 characters.

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

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


#2222

FromSimon Harrison <simon@simonharrison.net>
Date2011-04-03 16:07 -0500
Message-ID<f0d3a146489f1236a6bfbf28999f8dc7@ruby-forum.com>
In reply to#2202
Thanks guys. More options to try and remember! That's one of the things 
that bugs me a bit about Ruby. TMTOWTDI.

each |film| film.first, film.last
each |film| film.join
each |label, volume| ...

My brain would like: if you have an array with subarrays acting as key, 
value pairs, do this to access them. But, how am I to to decide which is 
the "best" option from the above? I suppose it depends on the situation. 
It becomes hard when every situation has multiple solutions and each 
solution may effect what to do next; which has multipe solutions etc.

Still, I've tried Perl, Python, Lisp, C, C++, C#, Java, VB and probably 
more, but Ruby seems to make sense more than all of these. At least I've 
achieved something, eh?

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

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


#2230

FromJosh Cheek <josh.cheek@gmail.com>
Date2011-04-03 17:36 -0500
Message-ID<BANLkTikvD0rJm6i22qmjuhE_BWKoiZiy_w@mail.gmail.com>
In reply to#2222
[Note:  parts of this message were removed to make it a legal post.]

On Sun, Apr 3, 2011 at 4:07 PM, Simon Harrison <simon@simonharrison.net>wrote:

> Thanks guys. More options to try and remember! That's one of the things
> that bugs me a bit about Ruby. TMTOWTDI.
>
> each |film| film.first, film.last
> each |film| film.join
> each |label, volume| ...
>
> My brain would like: if you have an array with subarrays acting as key,
> value pairs, do this to access them. But, how am I to to decide which is
> the "best" option from the above? I suppose it depends on the situation.
> It becomes hard when every situation has multiple solutions and each
> solution may effect what to do next; which has multipe solutions etc.
>
> Still, I've tried Perl, Python, Lisp, C, C++, C#, Java, VB and probably
> more, but Ruby seems to make sense more than all of these. At least I've
> achieved something, eh?
>
> --
> Posted via http://www.ruby-forum.com/.
>
>
I would suggest the last one "each |label, volume|" as it is the most
explicit. If you look at that code, you have a much better idea what data
you are dealing with. If you want the label: just use label, the volume:
volume. In the first two, you will have to look around your code, or run
some experiment to see that film is a two element Array with the first
element being the label and the second being the volume, it is only a little
more obscure, but little things add up.

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


#2266

FromRob Biedenharn <Rob@AgileConsultingLLC.com>
Date2011-04-04 07:42 -0500
Message-ID<3B9C4840-8738-4DDB-86D4-93FB163AFC7B@AgileConsultingLLC.com>
In reply to#2222
On Apr 3, 2011, at 5:07 PM, Simon Harrison wrote:

> Thanks guys. More options to try and remember! That's one of the  
> things
> that bugs me a bit about Ruby. TMTOWTDI.
>
> each |film| film.first, film.last
> each |film| film.join
> each |label, volume| ...
>
> My brain would like: if you have an array with subarrays acting as  
> key,
> value pairs, do this to access them.

You should look at the Array#assoc method that lets you do exactly this.

http://www.ruby-doc.org/core/classes/Array.html#M000269

-Rob

P.S. Sorry if this was mentioned already, I know I'm jumping into the  
middle of this conversation.

> But, how am I to to decide which is
> the "best" option from the above? I suppose it depends on the  
> situation.
> It becomes hard when every situation has multiple solutions and each
> solution may effect what to do next; which has multipe solutions etc.
>
> Still, I've tried Perl, Python, Lisp, C, C++, C#, Java, VB and  
> probably
> more, but Ruby seems to make sense more than all of these. At least  
> I've
> achieved something, eh?
>
> -- 
> Posted via http://www.ruby-forum.com/.
>

Rob Biedenharn		
Rob@AgileConsultingLLC.com	http://AgileConsultingLLC.com/
rab@GaslightSoftware.com		http://GaslightSoftware.com/

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


#2224

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-03 16:28 -0500
Message-ID<33013c139cf7d348769516eb971ecea6@ruby-forum.com>
In reply to#2202
Jesus christ.  Beginning ruby is a breeze compared to C++.  And you 
should see some perl in ruby.  In any language, there are always 
multiple ways to accomplish things.   With experience you will hit on 
the simplest way.  In fact, this board is really a competition to post 
the simplest way to do something.

Personally, I prefer python to all others.

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

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


#2225

FromSimon Harrison <simon@simonharrison.net>
Date2011-04-03 16:33 -0500
Message-ID<7a85d35ad3ad6276b5d8013573f36df2@ruby-forum.com>
In reply to#2202
I think the main thing that puts me off Python is having to import 
everything:

import os
import re
import sys

and things don't always live where I think they should live. Zed Shaw 
summed Python up here:

http://zedshaw.com/blog/2009-05-29.html

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

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


#2226

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-03 16:35 -0500
Message-ID<d3a595d0193e5b0258145cf8e8f80ed2@ruby-forum.com>
In reply to#2202
In ruby, the equivalent is require().  No difference.

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

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


#2229

FromSimon Harrison <simon@simonharrison.net>
Date2011-04-03 16:51 -0500
Message-ID<b476307a578d7e286fca0b401bd746ca@ruby-forum.com>
In reply to#2202
Personally, I think that if Python had succeeded in it's aim, then I'd 
be using it. The problem is that it has conceptions about where things 
should be kept that only make sense to Python programmers.

In Ruby most file operations are in the File class. Those that aren't 
should be found in the FileUtils class. Directories: try the Dir class. 
That makes sense to me. Not scattered around Os, Sys, Shutils, Fnutils 
and so on.

Python has a great philosophy but to a beginner can seem a bit of a 
mess. No offence intended. That's just my view.

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

[toc] | [prev] | [standalone]


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


csiph-web