Sponsored Content
Top Forums Shell Programming and Scripting Array not printing values if used in a loop Post 303017190 by arcoleman10 on Thursday 10th of May 2018 11:52:11 AM
Old 05-10-2018
Array not printing values if used in a loop

Hello!

I'm making an English to Morse Code translator and I was able to mostly get it all working by looking through older posts here; however, I have one small problem.

When I run it it's just printing spaces for where the characters should be. It runs the right amount of times, and if I try to individually print out of the array it works, but when I try to have it cycle through to translate I get the problem.

What I am running/output:
Code:
[acolem26@ats-cis project8]$ ./morse "a b c"
 SP  SP  EOT

My code:
Code:
#!/bash/bin

declare -A code
code[A]=".-"
code[B]="-..."
code[C]="-.-."
code[D]="-.."
code[E]="."
code[F]="..-."
code[G]="--."
code[H]="...."
code[I]=".."
code[J]=".---"
code[K]="-.-"
code[L]=".-.."
code[M]="--"
code[N]="-."
code[O]="---"
code[P]=".--."
code[Q]="--.-"
code[R]=".-."
code[S]="..."
code[T]="-"
code[U]="..-"
code[V]="...-"
code[W]=".--"
code[X]="-..-"
code[Y]="-.--"
code[Z]="--.."
code[1]=".----"
code[2]="..---"
code[3]="...--"
code[4]="....-"
code[5]="....."
code[6]="-...."
code[7]="--..."
code[8]="---.."
code[9]="----."
code[0]="-----"
code[ ]="SP"

str=$1

for (( i=0; $i < ${#str}; i++ ));
do
     printf "%s " "${code[${str:$i:1}]}"
done
printf "%s\n" EOT

If I do something like

Code:
printf "%s " "${code[A]}"

It prints off the corresponding Morse code so I assume the issue isn't with the array but with how I am substituting and I'm not sure how to fix it.
 

10 More Discussions You Might Find Interesting

1. Programming

printing all array values using dbx 7.2.1

how do we print the entire contents of arrays in dbx ? Ive tried using print x to print values 1 to 5 of the array x, however dbx complains and doesnt allow this help is much appreciated (1 Reply)
Discussion started by: JamesGoh
1 Replies

2. Shell Programming and Scripting

How to use array values after the loop.

- I m retreving values from database and wish to use those values later in my shell script. I m placing these values in an array da_data but outside loop array is empty.Problem is its treating array as local inside loop hence array is empty outside loop. Plz go through the script and suggest how... (1 Reply)
Discussion started by: Devesh5683
1 Replies

3. Shell Programming and Scripting

Array Printing Inline

Dear friends , The output file of below script Pls#!/bin/sh awk '{ bo = substr($0,13,3) slm = substr($0,150,8) slo = substr($0,175,7) inc = substr($0,97,10)/100 busi = substr($0,83,10) mth = substr($0,39,2) yer = substr($0,35,4) ... (2 Replies)
Discussion started by: vakharia Mahesh
2 Replies

4. Shell Programming and Scripting

Assigning values to an array via for/while loop

I need to do something like this: for i in 1 2 3 4 5; do arr=$(awk 'NR="$i" { print $2 }' file_with_5_records) done That is, parse a file and assign values to an array in an ascending order relative to the number of record in the file that is being processed on each loop. Is my... (2 Replies)
Discussion started by: fiori_musicali
2 Replies

5. Shell Programming and Scripting

PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array.

Hi All, I'm writing a nagios check that will see if our ldap servers are in sync... I got the status data into a nested array, I would like to search key of each array and if "OK" is NOT present, echo other key=>values in the current array to a variable so...eg...let take the single array... (1 Reply)
Discussion started by: zeekblack
1 Replies

6. UNIX for Dummies Questions & Answers

printing array elements

Is there a way to print multiple array elements without iterating through the array using bash? Can you do something like... echo ${array}and get all those separate elements from the array? (2 Replies)
Discussion started by: jrymer
2 Replies

7. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

8. Shell Programming and Scripting

awk loop using array:wish to store array values from loop for use outside loop

Here's my code: awk -F '' 'NR==FNR { if (/time/ && $5>10) A=$2" "$3":"$4":"($5-01) else if (/time/ && $5<01) A=$2" "$3":"$4-01":"(59-$5) else if (/time/ && $5<=10) A=$2" "$3":"$4":0"($5-01) else if (/close/) { B=0 n1=n2; ... (2 Replies)
Discussion started by: klane
2 Replies

9. Shell Programming and Scripting

How to swap the values in array using for loop?

array=( 8 5 6 2 3 4 7 1 9 0 ) for i in "${array}" do echo $i done # i need the output like this by swapping of array values 0 9 1 7 4 3 2 6 5 8 (7 Replies)
Discussion started by: Meeran Rizvi
7 Replies

10. Shell Programming and Scripting

Convert values in an array using a loop

I have a headerless array of 1000 columns x 100000 rows. The array only contains 4 values; 0/0, 0/1, 1/1, ./. Here I am showing the 1st 3 rows and columns of the input array 0/0 0/0 1/1 0/1 0/1 0/1 0/0 ./. 0/0 0/0 0/0 0/0 I would like to convert the values in... (9 Replies)
Discussion started by: Geneanalyst
9 Replies
HTML::Template(3ruby)													     HTML::Template(3ruby)

NAME
HTML::Template - Ruby extension for HTML::Template DESCRIPTION
This module deals with HTML template by CGI scripts and use a complete selectivity between design and logic easily. You can deal with a nested block, a loop and a conditional expression. Simple example, see below. template file test.html <HTML> <HEAD><TITLE>Test Template</TITLE> <BODY> My Home Directory is <!var:home> <P> My Path is set to <!var:path> </BODY> </HTML> Ruby script require "html/template" tmpl = HTML::Template.new("./test.html") tmpl.param({ 'home' => ENV['HOME'], 'path' => ENV['PATH'] }) print "Content-Type: text/html " print tmpl.output INSTALL
using install.rb % tar xvzf html-template-x.xx.tar.gz % cd html-template-x.xx % ruby install.rb config % ruby install.rb setup % su # ruby install.rb install Template File TAGS
You can use original style tag or valid HTML comment style tag. <!var:PARAMETER_NAME> <!-- var:PARAMETER_NAME --> When you call HTML::Template#param ({PARAMETER_NAME => VAL}), tags will be simply replaced to VAL. <!begin:PARAMETER_NAME> <!end:PARAMETER_NAME> <!-- begin:PARAMETER_NAME --> <!-- end:PARAMETER_NAME --> It allows you to separate a section of text and give it a name It would be able to make them loop or conditional expression. And, it can be nested. <!include:FILE_NAME> <!-- include:FILE_NAME --> A file specified by FILE_NAME is simply inserted to a template file. The template file will be treated as a merged template. If you pass the path param to HTML::Template.new , include file will be searched under the path. HTML
::Template CLASS CLASS METHODS
HTML::Template.new(params = {}) Creates a HTML::Template object. Given a file name to its first argument, use this file as a template. Also you can pass params as Hash. The options are below o filename template filename o path template search path. you can specified as String or Array of String. METHODS
HTML::Template#load(file) Load template file. HTML::Template#set_html(html) Setting up a string as a template. HTML::Template#param(hash = {}) HTML::Template#expand(hash = {}) Specifies a string or a loop by Hash for a substitute. When specifying more than a loop simultaneously, value of the hash becomes an array. HTML::Template#node(name) HTML::Template#loop(name) HTML::Template#cond(name) Stands a loop and a conditional expression specified by <!begin:name><!end:name> Retrieves HTML::TemplateNode object. HTML::Template#output HTML::Template#to_s Returns the final result. HTML
::TemplateNode CLASS This is a class for a loop or a conditional expression. An instance consists of HTML::Template Class and HTML::TemplateNode CLASS. METHODS
HTML::TemplateNode#node(name) Stands a loop or a conditional expression specified by <!begin:name><!end:name> Returns HTML::TemplateNode object. Is only used for nested. HTML::TemplateNode#param(hash = {}) HTML::TemplateNode#expand(hash = {}) HTML::TemplateNode#add(hash = {}) Specifies a string or a loop by Hash for a substitute. If you don't call the method, the block won't be displayed. A LOOP
This sample is a displaying list of environment valiables. <html> <body> <h1>Env List</h1> <hr> <!begin:envlist> <!var:key> : <!var:val><br> <!end:envlist> <hr> </body> </html> # In case of using it with iterator and replace data. require "html/template" tmpl = HTML::Template.new("filename" => "envlist.html") ENV.keys.sort.each do |k| tmpl.node("envlist").param({'key' => k, 'val' => ENV[k] }) end print "Content-Type: text/html " print tmpl.output # In case of giving a loop as an array require "html/template" tmpl = HTML::Template.new("envlist.html") envlist = [] ENV.keys.sort.each do |k| envlist.push({'key' => k, 'val' => ENV[k] }) end tmpl.param({'envlist' => envlist}) print "Content-Type: text/html " print tmpl.output A Conditional Expression <html> <body> <!begin:true> This is True <!var:foo> <hr> <!end:true> <!begin:false> This is False <!var:foo> <hr> <!end:false> </body> </html> require "html/template" tmpl = HTML::Template.new("cond.html") tmpl.param({'true' => {'foo' => 'foobar'} }) print "Content-Type: text/html " print tmpl.output The performed result is below, however, a part not specied with the method isn't displayed. <html> <body> This is True foobar <hr> </body> </html> An attached Sample. env.rb This sample is a simply replacing valiables. envlist.rb This sample is a displaying list of environment valiables. This sample is a case for using with iterator. envlist2.rb Performed result is the same as the envlist.rb.'s. nest.rb A sample of a nested loop. THANKS
English Documentation NAKAYAMA Nao <nao_o@netlaputa.ne.jp> thanks a lot :-) AUTHOR
Copyright 2001 IKEBE Tomohiro This library is free software; you can redistribute it and / or modify it under the same terms as Ruby itself. IKEBE Tomohiro <ikechin@Oxfa.com> June 2002 HTML::Template(3ruby)
All times are GMT -4. The time now is 04:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy