Sponsored Content
Full Discussion: Array question
Top Forums UNIX for Dummies Questions & Answers Array question Post 302753973 by bakunin on Wednesday 9th of January 2013 08:51:21 PM
Old 01-09-2013
The problem is the behavior of the "for"-loop in connection with an obvious misunderstanding about what "${arr[*]}" means.

The subscript "*" in an array denotes ALL array elements and it is expanded before the for-loop is executed. Therefore this is what the shell "sees":

Code:
for i in ${arr[*]}            # original line, begin of parsing process
for i in hello my name is     # after expanding the variables

Now, "$i" is assigned one word each pass of the for-loop because this is how this kind of loop behaves. There are two possibilities to correct this:

1. Use a while-loop instead:

Code:
echo ${arr[*]} | while read a ; do
     echo $a
done

2. Instead of using this faulty way of expanding the array count elements:

Code:
i=1
for i in {1..${#arr[*]}} ; do
     echo ${arr[$i]}
done

"${#arrayname[*]}" expands to the number of elements in your array (instead of the whole array itself), in your case: 2.

I hope this helps.

bakunin
 

10 More Discussions You Might Find Interesting

1. Filesystems, Disks and Memory

Storage array question

We just purchased a MOD30 disk array strage system. We have 15 drives and 2 hot spares. We're running a database app with 8 data sets. I'm trying to get the best i/o speed out of my disk configuration. Right now I have 3 raid5 arrays setup. This seems to offer the same performance as having the... (1 Reply)
Discussion started by: ncmurf00
1 Replies

2. UNIX for Dummies Questions & Answers

Array question

When setting a variable, how would I go about making each result a new line? A very simple example would be: theFolders=`(ls -l /)` echo $theFolders This gives me all the folders as one variable and I need to be able to use each as its own variable. I'm sure I have to make this into an... (2 Replies)
Discussion started by: TheCrunge
2 Replies

3. Shell Programming and Scripting

Array question

Hi all, I have a question does anyone know if it is possible to push or pop an array in the ksh environment? Could anyone give me a hint, because I am trying to merge 2 server files together and there are some names in the server is not proper anymore. Thank you in advance. (4 Replies)
Discussion started by: ahtat99
4 Replies

4. Shell Programming and Scripting

perl array question from going through hash

suppose my @{$data1{$callid}}; cotains one two three three five six one two three of random patterns but each item is separated by white space or tab, Below code extract and get rid of the whitespace perfectly so that it shows now like this onetwothree threefivesix... (2 Replies)
Discussion started by: hankooknara
2 Replies

5. Shell Programming and Scripting

Variable Sized Array Length Question

I need to implement the following logic and need some expert help from UNIX community. These are the steps in my Shell script. 1. Analyze a file. 2. Extract all the ID's in that file. 3. Use the ID's from #2 to run another filter on the file. I've implemented # 1 and 2 using... (3 Replies)
Discussion started by: katwala
3 Replies

6. Shell Programming and Scripting

Help! Yet another check element in array Question

Greetings, DISCLAIMER: My shell scripting is rusty so my question may be borderline stupid. You've been warned. I need to create a script that a) lists the content of zip files in a directory and b) sends out an `exception` report. My ZIP files contain a control file (for load check). I want... (2 Replies)
Discussion started by: alan
2 Replies

7. Programming

array question

Im new to C programming and am having trouble understanding the output of this code int array={4,5,8,9,8,1,0,1,9,3}; int *array_ptr; int main() { array_ptr=array; while((*array_ptr) != 0) array_ptr++;; printf("%d\n", array_ptr - array); return(0); } the output is 6 but I... (2 Replies)
Discussion started by: sacat
2 Replies

8. Shell Programming and Scripting

Question on iterating array elements

Hi, I am trying to do something similar to the for loop example from KSH For Loop Array: Iterate Through Array Values $: cat y.ksh #!/bin/ksh # set array called nameservers set -A nameservers 192.168.1.1 192.168.1.5 202.54.1.5 # print all name servers for i in ${nameservers} do ... (3 Replies)
Discussion started by: newbie_01
3 Replies

9. Shell Programming and Scripting

Zsh array -a vs. -A question

Inside a zsh function, I create a local array with local -a arrayname and a local associative array with local -A arrayname. I also can create an array using set, like this: set -A arrayname value1 value2 value3In this form, I can not explicitly declare that an array is associative or... (2 Replies)
Discussion started by: rovf
2 Replies

10. Shell Programming and Scripting

Associative array index question

I am trying to assign indexes to an associative array in a for loop but I have to use an eval command to make it work, this doesn't seem correct I don't have to do this with regular arrays For example, the following assignment fails without the eval command: #! /bin/bash read -d "\0" -a... (19 Replies)
Discussion started by: Riker1204
19 Replies
Graph(3pm)						User Contributed Perl Documentation						Graph(3pm)

NAME
SVG::Graph - Visualize your data in Scalable Vector Graphics (SVG) format. SYNOPSIS
use SVG::Graph; use SVG::Graph::Data; use SVG::Graph::Data::Datum; #create a new SVG document to plot in... my $graph = SVG::Graph->new(width=>600,height=>600,margin=>30); #and create a frame to hold the data/glyphs my $frame = $graph->add_frame; #let's plot y = x^2 my @data = map {SVG::Graph::Data::Datum->new(x=>$_,y=>$_^2)} (1,2,3,4,5); my $data = SVG::Graph::Data->new(data => @data); #put the xy data into the frame $frame->add_data($data); #add some glyphs to apply to the data in the frame $frame->add_glyph('axis', #add an axis glyph 'x_absolute_ticks' => 1, #with ticks every one #unit on the x axis 'y_absolute_ticks' => 1, #and ticks every one #unit on the y axis 'stroke' => 'black', #draw the axis black 'stroke-width' => 2, #and 2px thick ); $frame->add_glyph('scatter', #add a scatterplot glyph 'stroke' => 'red', #the dots will be outlined #in red, 'fill' => 'red', #filled red, 'fill-opacity' => 0.5, #and 50% opaque ); #print the graphic print $graph->draw; DESCRIPTION
SVG::Graph is a suite of perl modules for plotting data. SVG::Graph currently supports plots of one-, two- and three-dimensional data, as well as N-ary rooted trees. Data may be represented as: Glyph Name Dimensionality supported 1d 2d 3d tree -------------------------------------------------------- Axis x Bar Graph x Bubble Plot x Heatmap Graph x Line Graph x Pie Graph x Scatter Plot x Spline Graph x Tree x SVG::Graph 0.02 is a pre-alpha release. Keep in mind that many of the glyphs are not very robust. PLOTTING
You need to create a SVG::Graph::Frame instance from the parent SVG::Graph instance for each set of data to be plotted. Datasets can be hierarchical, and to represent this, SVG::Graph::Frame instances can themselves contain subframes. SVG::Graph::Frame can contain: - multiple subframes as instances of SVG::Graph::Frame - a single SVG::Graph::Data instance - multiple SVG::Graph::Glyph instances with which to render the attached SVG::Graph::Data instance, and all SVG::Graph::Data instances attached to SVG::Graph::Frame subinstances See SVG::Graph::Frame and SVG::Graph::Glyph for details. ONE DATA SET 1. create an SVG::Graph instance 2. create an SVG::Graph::Frame instance by calling SVG::Graph::add_frame(); 3. create an SVG::Graph::Data instance, containing an SVG::Graph::Data::Datum instance for each data point. 4. Attach the SVG::Graph::Data instance to your SVG::Graph::Frame using SVG::Graph::Frame::add_data(); 5. Attach glyphs to the SVG::Graph::Frame instance using SVG::Graph::Frame::add_glyph(); 6. Call SVG::Graph::draw(); MULTIPLE DATA SETS 1. create an SVG::Graph instance 2. create an SVG::Graph::Frame instance by calling SVG::Graph::add_frame(); 3. create an SVG::Graph::Data instance, containing an SVG::Graph::Data::Datum instance for each data point. 4. Attach the SVG::Graph::Data instance to your SVG::Graph::Frame using SVG::Graph::Frame::add_data(); 5. Attach glyphs to the SVG::Graph::Frame instance using SVG::Graph::Frame::add_glyph(); 6. repeat [2-5] for each additional data set to be added. add_frame() can be called on SVG::Graph to add top-level data sets, or SVG::Graph::Frame to add hierarchical data sets. 7. Call SVG::Graph::draw(); FEEDBACK
Send an email to the svg-graph-developers list. For more info, visit the project page at http://www.sf.net/projects/svg-graph AUTHORS
Allen Day, <allenday@ucla.edu> Chris To, <crsto@ucla.edu> CONTRIBUTORS
James Chen, <chenj@seas.ucla.edu> Brian O'Connor, <boconnor@ucla.edu> SEE ALSO
SVG METHODS
new Title : new Usage : my $graph = SVG::Graph->new(width=>600, height=>600, margin=>20); Function: creates a new SVG::Graph object Returns : a SVG::Graph object Args : width => the width of the SVG height => the height of the SVG margin => margin for the root frame init Title : init Usage : Function: Example : Returns : Args : width Title : width Usage : $obj->width($newval) Function: Example : Returns : value of width (a scalar) Args : on set, new value (a scalar or undef, optional) height Title : height Usage : $obj->height($newval) Function: Example : Returns : value of height (a scalar) Args : on set, new value (a scalar or undef, optional) margin Title : margin Usage : $obj->margin($newval) Function: Example : Returns : value of margin (a scalar) Args : on set, new value (a scalar or undef, optional) svg Title : svg Usage : $obj->svg($newval) Function: Example : Returns : value of svg (a scalar) Args : on set, new value (a scalar or undef, optional) add_frame Title : add_frame Usage : my $frame = $graph->add_frame Function: adds a Frame to the current Graph Returns : a SVG::Graph::Frame object Args : a hash. usable keys: frame_transform (optional) 'top' default orientation 'bottom' rotates graph 180 deg (about the center) 'right' points top position towards right 'left' points top position towards left frames Title : frames Usage : get/set Function: Example : Returns : Args : xoffset Title : xoffset Usage : $obj->xoffset($newval) Function: Example : Returns : value of xoffset (a scalar) Args : on set, new value (a scalar or undef, optional) yoffset Title : yoffset Usage : $obj->yoffset($newval) Function: Example : Returns : value of yoffset (a scalar) Args : on set, new value (a scalar or undef, optional) draw Title : draw Usage : $graph=>draw Function: depends on child glyph implementations Returns : xmlifyied SVG object Args : none perl v5.10.0 2009-01-07 Graph(3pm)
All times are GMT -4. The time now is 09:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy