remove array bracket

  • Thread starter Kamarulnizam Rahim
  • Start date
K

Kamarulnizam Rahim

Hi when i run my script, the output is as followed:

[{"name"=>"Efficiency (Energy)",
"type"=>"Objective",
"subtype"=>"NIZAMMMMzz",
"components"=>
[{"type"=>"ContentBox", "title"=>"Audit",
"args"=>{:content=>"None\n"}},
{"type"=>"ChildListingComponent",
"title"=>"Current Targets for the Efficiency (Energy)
Objective:"}]},
{"children"=>
[{"name"=>"Routinely monitor energy use",
"type"=>"Target",
"subtype"=>"ATEEN",
"components"=>
[{"type"=>"ContentBox",
"title"=>"Summary",
"args"=>{:content=>"None\n"}},
{"type"=>"ContentBox", "title"=>"Description",
"args"=>{:content=>""}},
{"type"=>"TargetComponent",
"title"=>"None",
"args"=>
{:start_date=>"#<Date: 2010-08-10 ",
:end_date=>"5/1/2011",
:state=>"Active"}}]}]},
...........
...........
:state=>"Active"}}]}]}]

How do i tell ruby to remove the very first and the very last bracket
'[...]' out from my output?

Nizam
 
J

Jiawei Yong

Are these output to a txt file, or irb? Mind sharing your output
formatting code?
 
J

Jeremy Bopp

Hi when i run my script, the output is as followed:

[{"name"=>"Efficiency (Energy)",
"type"=>"Objective",
"subtype"=>"NIZAMMMMzz",
"components"=>
[{"type"=>"ContentBox", "title"=>"Audit",
"args"=>{:content=>"None\n"}},
{"type"=>"ChildListingComponent",
"title"=>"Current Targets for the Efficiency (Energy)
Objective:"}]},
{"children"=>
[{"name"=>"Routinely monitor energy use",
"type"=>"Target",
"subtype"=>"ATEEN",
"components"=>
[{"type"=>"ContentBox",
"title"=>"Summary",
"args"=>{:content=>"None\n"}},
{"type"=>"ContentBox", "title"=>"Description",
"args"=>{:content=>""}},
{"type"=>"TargetComponent",
"title"=>"None",
"args"=>
{:start_date=>"#<Date: 2010-08-10 ",
:end_date=>"5/1/2011",
:state=>"Active"}}]}]},
...........
...........
:state=>"Active"}}]}]}]

How do i tell ruby to remove the very first and the very last bracket
'[...]' out from my output?

How does your script produce that output in the first place? I'm going
to assume that you have a reference to an array and that you're printing
the output of that array's inspect method. In that case, you could try
something like this:

array = ... # Array value from somewhere
puts array.map(&:inspect).join(", ")

This code maps each element of the array to the value of its associated
inspect method and then joins all elements of the array with ", ". You
should be able to use similar logic if you are using something other
than the inspect method as well.

-Jeremy
 
K

Kamarulnizam Rahim

Hi guys,

Actually i just want to remove the bracket (The very first and last
bracket) because if the bracket still there, it will affect the
indentation on my yaml output (as the posted output above will be
converted into yaml format). This is what will happen on my yaml output
if i do not remove the brackets:

- - name: Efficiency (Energy)
type: Objective
subtype: NIZAMMMMzz
components:
- type: ContentBox
title: Audit
args:
:content: |
None

- type: ChildListingComponent
title: "Current Targets for the Efficiency (Energy)
Objective:"
- children:
- name: Routinely monitor energy use
type: Target
subtype: ATEEN
components:
- type: ContentBox
title: Summary
args:
:content: |
None

As you can see, there are double indented dash (- -) on the Efficiency
(energy) and i want to remove one of those.This is the code i use:

tar = [ ]
objective = {"name"=>"#{blank}",
"type"=>"Objective",
"subtype"=>"NIZAMMMMzz",
"components"=>
[{"type"=>"ContentBox",
"title"=>"Audit",
"args"=>{:content=>"None\n"}},
{"type"=>"ChildListingComponent",
"title"=>"Current Targets for the #{blank} Objective:"}]}
tar << objective

mon = { "children"=>
[{"name"=>"#{question}",
"type"=>"Target",
"subtype"=>"ATEEN",
"components"=>
[{"type"=>"ContentBox",
"title"=>"Summary",
"args"=>{:content=>"None\n"}},
{"type"=>"ContentBox",
"title"=>"Description",
"args"=>
{:content=>
""}},
{"type"=>"TargetComponent",
"title"=>"None",
"args"=>
{:start_date=>"#<Date: 2010-08-10 ",
:end_date=>"#{due_date}",
:state=>"#{status}"}}]}]}
tar << mon
pp tar

Nizam
 
J

Jeremy Bopp

Hi guys,

Actually i just want to remove the bracket (The very first and last
bracket) because if the bracket still there, it will affect the
indentation on my yaml output (as the posted output above will be
converted into yaml format). This is what will happen on my yaml output
if i do not remove the brackets:

- - name: Efficiency (Energy)
type: Objective
subtype: NIZAMMMMzz
components:
- type: ContentBox
title: Audit
args:
:content: |
None

- type: ChildListingComponent
title: "Current Targets for the Efficiency (Energy)
Objective:"
- children:
- name: Routinely monitor energy use
type: Target
subtype: ATEEN
components:
- type: ContentBox
title: Summary
args:
:content: |
None

As you can see, there are double indented dash (- -) on the Efficiency
(energy) and i want to remove one of those.This is the code i use:

tar = [ ]
objective = {"name"=>"#{blank}",
"type"=>"Objective",
"subtype"=>"NIZAMMMMzz",
"components"=>
[{"type"=>"ContentBox",
"title"=>"Audit",
"args"=>{:content=>"None\n"}},
{"type"=>"ChildListingComponent",
"title"=>"Current Targets for the #{blank} Objective:"}]}
tar << objective

mon = { "children"=>
[{"name"=>"#{question}",
"type"=>"Target",
"subtype"=>"ATEEN",
"components"=>
[{"type"=>"ContentBox",
"title"=>"Summary",
"args"=>{:content=>"None\n"}},
{"type"=>"ContentBox",
"title"=>"Description",
"args"=>
{:content=>
""}},
{"type"=>"TargetComponent",
"title"=>"None",
"args"=>
{:start_date=>"#<Date: 2010-08-10 ",
:end_date=>"#{due_date}",
:state=>"#{status}"}}]}]}
tar << mon
pp tar

The variable tar is an array of hashes, and the first output you posted
agrees with this. At this point there does not appear to be any cause
for an added array wrapping tar. You need to show us how you output the
value of tar into your YAML file. You are likely wrapping tar within
another array before you do that.

-Jeremy
 
K

Kamarulnizam Rahim

class EnergyManagement < Environmental
def initialize(title)
@title = title
end
def objective
convert_yaml = Environmental.yaml #YAML::load_file('nizam.yaml')
convert_yaml["System"]["Environmental"]["children"][2]["children"]
<< @title
File.open("nizam_out.yaml", "w"){|f| YAML.dump(convert_yaml, f)}
end
end

e = EnergyManagement.new(tar)
e.objective
 
J

Jeremy Bopp

class EnergyManagement < Environmental
def initialize(title)
@title = title
end
def objective
convert_yaml = Environmental.yaml #YAML::load_file('nizam.yaml')
convert_yaml["System"]["Environmental"]["children"][2]["children"]
<< @title
File.open("nizam_out.yaml", "w"){|f| YAML.dump(convert_yaml, f)}
end
end

e = EnergyManagement.new(tar)
e.objective

At this point I can't see where the problem is since none of this
correlates in an obvious way with your earlier posts. I think you need
to simplify this whole thing into a basic test case using a stripped
down and much simplified YAML file that reproduces the core problem.
That way those of us here can try out your code, see exactly what you
see, and ask you more intelligent question. :)

-Jeremy
 
P

Phillip Gawlowski

class EnergyManagement < Environmental

What does the Environmental class look like?

And what does the input YAML look like?

--
Phillip Gawlowski

Though the folk I have met,
(Ah, how soon!) they forget
When I've moved on to some other place,
There may be one or two,
When I've played and passed through,
Who'll remember my song or my face.
 
K

Kamarulnizam Rahim

class Environmental
@@convert_yaml = nil
def self.yaml
if @@convert_yaml.nil? then
@@convert_yaml = YAML::load_file('nizam.yaml')
end
@@convert_yaml
end
end

------------------------------------------------------------------
- name: Optimisation
type: Objective
subtype: None
components:
- type: ContentBox
title: Optimisation
args:
:content: |
None

- type: ChildListingComponent
title: "Current Targets for the Optimisation Objective:"
-------------------------------------------------------------------
- - name: Efficiency (Energy)
type: Objective
subtype: NIZAMMMzzz
components:
- type: ContentBox
title: Team
args:
:content: |
None

the input of my yaml file is similar as data within the dashed line. The
thing is, by eliminating the brackets as i mentioned earlier will
eliminate the double dashes (- - name: Efficiency (Energy)), hence
corrected the indentation on my yaml output file. I just dnt know how to
eliminate that bracket since i added two hashes ('objective' and 'mon')
into an array ('tar'). Is there any other way i can add those two hashes
without inserting it into an array? My desire yaml output file is like
this:

- name: Optimisation
type: Objective
subtype: None
components:
- type: ContentBox
title: Optimisation
args:
:content: |
None

- type: ChildListingComponent
title: "Current Targets for the Optimisation Objective:"

- name: Efficiency (Energy)
type: Objective
subtype: NIZAMMMzzz
components:
- type: ContentBox
title: Team
args:
:content: |
None

Thanks

Nizam
 
J

Jeremy Bopp

class Environmental
@@convert_yaml = nil
def self.yaml
if @@convert_yaml.nil? then
@@convert_yaml = YAML::load_file('nizam.yaml')
end
@@convert_yaml
end
end

Given the way you used this class within the EnergyManagement class,
there is no need to make EnergyManagement inherit from Environmental.
Just FYI.
------------------------------------------------------------------
- name: Optimisation
type: Objective
subtype: None
components:
- type: ContentBox
title: Optimisation
args:
:content: |
None

- type: ChildListingComponent
title: "Current Targets for the Optimisation Objective:"
-------------------------------------------------------------------
- - name: Efficiency (Energy)
type: Objective
subtype: NIZAMMMzzz
components:
- type: ContentBox
title: Team
args:
:content: |
None

the input of my yaml file is similar as data within the dashed line. The
thing is, by eliminating the brackets as i mentioned earlier will
eliminate the double dashes (- - name: Efficiency (Energy)), hence
corrected the indentation on my yaml output file. I just dnt know how to
eliminate that bracket since i added two hashes ('objective' and 'mon')
into an array ('tar').

Let's fix some terminology here just to prevent potential confusion.
You don't want to eliminate brackets. You want to extract these items
from the array in which they are embedded. "Eliminating brackets" is
something you would do directly to the string output generated by
something like the inspect method. The apparent extra array is the root
of the problem rather than the brackets shown when you inspect it. ;-)
Is there any other way i can add those two hashes
without inserting it into an array? My desire yaml output file is like
this:

- name: Optimisation
type: Objective
subtype: None
components:
- type: ContentBox
title: Optimisation
args:
:content: |
None

- type: ChildListingComponent
title: "Current Targets for the Optimisation Objective:"

- name: Efficiency (Energy)
type: Objective
subtype: NIZAMMMzzz
components:
- type: ContentBox
title: Team
args:
:content: |
None

OK. It's really hard to be sure here, but I think the problem is that
you need to convert this section of your code:

convert_yaml["System"]["Environmental"]["children"][2]["children"]
<< @title

To this:

convert_yaml["System"]["Environmental"]["children"][2]["children"] += @title

The issue is that tar is an array of hashes, but it appears that you
actually want to append the contents of tar to the data structure you
have from the YAML file instead of appending tar itself. This code
appends the contents of the array in tar to the existing array in that
mess of hashes.

There are other ways to handle this, but this one is the simplest
change. The downside is that you would prevent someone actually
appending tar as-is if they actually wanted to do so. Maybe that's not
an issue for this code.

-Jeremy
 
K

Kamarulnizam Rahim

Thanks Jeremy,

I am able to solve the problem regarding indentation by changing the
code as posted. There is one little question i want to ask.

How do convert these two hashes:

[{xxxxxx},{yyyyyy}]

to be like this:

[{xxxxxx, yyyyyy}]

Is it possible at all?

Regarding the Environmental class. I still need that class because i
want to open my yaml file to be used by many subclasses, not just
EnergyManagement only. Thanks

Nizam
 
K

Kamarulnizam Rahim

Btw there is a catch, {yyyyyy} hash is created based on a looping method
(CSV.foreach) and i dont think using common inject, collect or merge
method is the right choice. I tried it, and it didnt work

Nizam
 
R

Robert Klemme

OK. =A0It's really hard to be sure here, but I think the problem is that
you need to convert this section of your code:

convert_yaml["System"]["Environmental"]["children"][2]["children"]
<< @title

To this:

convert_yaml["System"]["Environmental"]["children"][2]["children"] +=3D @=
title

I'd rather

convert_yaml["System"]["Environmental"]["children"][2]["children"].concat(@=
title)
The issue is that tar is an array of hashes, but it appears that you
actually want to append the contents of tar to the data structure you
have from the YAML file instead of appending tar itself. =A0This code
appends the contents of the array in tar to the existing array in that
mess of hashes.

There are other ways to handle this, but this one is the simplest
change. =A0The downside is that you would prevent someone actually
appending tar as-is if they actually wanted to do so. =A0Maybe that's not
an issue for this code.

Another remark: I don't understand why indentation in a YAML file is
such an issue. After all it's a data format and readable either way.
Typically too few indentation is more an issue than too much. If the
formatting is that crucial it might be worthwhile to look into writing
a new YAML formatter.

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
K

Kamarulnizam Rahim

Hi Jeremy,

I have already solved the problem by myself by manipulating bit of the
hash content. Its quite easy though. Maybe i think of the wrong way
before this. If you are still interested, i can show you how i done it.
Cheers

Nizam
 
J

Jeremy Bopp

Hi Jeremy,

I have already solved the problem by myself by manipulating bit of the
hash content. Its quite easy though. Maybe i think of the wrong way
before this. If you are still interested, i can show you how i done it.

What's important is that you fixed your problem and that you understand
how you did it. If you have more questions, please post them though.

-Jeremy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,807
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top