Perl Format: Not enough format arguments - perl

I am currently having trouble with formatting my output. I understand that this may be "bad practice", but using format is a requirement.
I am attempting to get a print out of the values that I retrieved from the database into a template of a SQL insert for Netcool.
I'm referencing http://perldoc.perl.org/perlform.html to apply the proper variables but I'm not sure why this is not working.
Here is a code snippet:
129 foreach my $row (#{$rows}) {
130
131 $trigger_name = removeNull($row->{triggername});
132 $trigger_group = $row->{groupname};
133 $trigger_kind = $row->{triggerkind};
134 $code_block = $row->{codeblock};
135 $isDebugEnabled = IntToLiteralBoolean($row->{debugenabled});
136 $isEnabled = IntToLiteralBoolean($row->{isenabled});
137 $priority = $row->{triggerpriority};
138 $comment_block = $row->{commentblock};
139 $evaluate_block = $row->{evaluateblock};
140 $bind_name = $row->{bindname};
141 $condition_block = $row->{conditionblock};
142 $declare_block = $row->{declareblock};
143
144 # my $sth_triggerType;
145
146 if($trigger_kind == 0) {
147 #database logic goes here
148 my $sql_getDatabaseFields = "select *
149 from catalog.database_triggers
150 where TriggerName = '$trigger_name'";
151
152 print "\n\nExtracting trigger: $trigger_name";
153 print "\nTriggerKind: Database, $trigger_kind";
154
155 my $sth_triggerType = $dbh->prepare($sql_getDatabaseFields);
156 $sth_triggerType->execute;
157 my $dbRows = $sth_triggerType->fetchall_arrayref({});
158
159 foreach my $row (#{$dbRows}) {
160 $event_order = $row->{eventorder};
161 $event_op = $row->{eventop};
162 $database_name = $row->{databasename};
163 $table_name = $row->{tablename};
164 $event_level = $row->{eventlevel};
165 write;
166
167 # print "\nEvent Order: $event_order";
168 # print "\nEvent Op: $event_op";
169 # print "\nDatabase Name:$database_name";
170 # print "\nTable name:$table_name";
171 # print "\nEvent Level: $event_level";
172 }
173 format =
174
175 CREATE OR REPLACE TRIGGER #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
176 $trigger_name
177 GROUP #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
178 $trigger_group
179 DEBUG #<<<<<<<<<<<<
180 $isDebugEnabled
181 ENABLED #<<<<<<<<<<<<
182 $isEnabled
183 PRIORITY #<<
184 $priority
185 COMMENT ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
186 $comment_block
187 ~~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
188 $comment_block
189 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
190 $comment_block
191 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
192 $comment_block
193 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
194 $comment_block
195 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
196
197 begin
198 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
199 $code_block
200 ~~^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
201 $code_block
202 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
203 $code_block
204 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
205 $code_block
206 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
207 $code_block
208 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
209 $code_block
210 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
211 $code_block
212 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
213 $code_block
214 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
215 $code_block
216 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
217 $code_block
218 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
219 $code_block
220 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
221 $code_block
222 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
223 $code_block
224 ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
225 $code_block
226 end;
227 .
This is the output when debugging:
Extracting trigger: service_reinsert
Not enough format arguments at nc_trigger_extract.pl line 199.
at nc_trigger_extract.pl line 199
main::STDOUT called at nc_trigger_extract.pl line 165
Modification of a read-only value attempted at nc_trigger_extract.pl line 199.
at nc_trigger_extract.pl line 199
main::STDOUT called at nc_trigger_extract.pl line 165
Debugged program terminated. Use q to quit or R to restart,
139: $evaluate_block = $row->{evaluateblock};
140: $bind_name = $row->{bindname};
141: $condition_block = $row->{conditionblock};
142==>b $declare_block = $row->{declareblock};
143
144 # my $sth_triggerType;
145
146: if($trigger_kind == 0) {
147 #database logic goes here
148: my $sql_getDatabaseFields = "select *
DB<2> x $code_block
0 "\cJ\cIset old.LastReportAt = new.LastReportAt;\cJ\cJ\cIif ( old.CurrentState != new.CurrentState )\cJ\cIthen\cJ\cI\cIset old.CurrentState = new.CurrentState;\cJ\cI\cIset old.StateChange = new.StateChange;\cJ\cIend if;\cJ\cJ\cIif ( new.CurrentState = 0 )\cJ\cIthen\cJ\cI\cIset old.LastGoodAt = new.LastGoodAt;\cJ\cIelseif ( new.CurrentState = 1 )\cJ\cIthen\cJ\cI\cIset old.LastMarginalAt = new.LastMarginalAt;\cJ\cIelseif ( new.CurrentState = 2 )\cJ\cIthen\cJ\cI\cIset old.LastBadAt = new.LastBadAt;\cJ\cIend if;\cJ\c#"
DB<4> x $comment_block
0 "Service processing for service.status\c#"
DB<5> c

Compare the following two examples:
1)
my $code_block = "helloworld";
my $str = "mars";
format =
^<<<<
$str
begin
^<<<<
$code_block
^<<<<
$code_block
^<<<<
$code_block
end;
.
write STDOUT;
--output:--
mars
begin
hello
world
end;
2)
my $code_block = "helloworld";
my $str = "mars";
format =
^<<<<
begin #LINE 11
^<<<<
$code_block
^<<<<
$code_block
^<<<<
$code_block
end;
.
write STDOUT;
--output:--
Modification of a read-only value attempted at 1.pl line 11.

Related

Why am I getting the error "Index exceeds matrix dimensions"?

I am currently new to MATLAB. My code is below. I just have a question regarding why I keep getting the error "Index exceeds matrix dimensions" for the functions provided:
a = [105 97 245 163 207 134 218 199 160 196 221 154 228 131 180 178 157 151 ...
175 201 183 153 174 154 190 76 101 142 149 200 186 174 199 115 193 167 ...
171 163 87 176 121 120 181 160 194 184 165 145 160 150 181 168 158 208 ...
133 135 172 171 237 170 180 167 176 158 156 229 158 148 150 118 143 141 ...
110 133 123 146 169 158 135 149];
mean = mean(a)
std = std(a)
max = max(a)
min = min(a)
range = range(a)
Don't give variables the same names as existing functions. This shadows the function. When you then try to call the function with an argument you instead end up indexing the variable with the argument, which in this case tries to index elements in the variable that don't exist, hence your error.
Use clear to remove the existing variables, then rerun the calculations with new variable names:
clear mean std max min range;
meanResult = mean(a);
stdResult = std(a);
...
Use clc (clear command window), clear (removes all variables from the workspace) and close all (closes off any previously used figures) to clean you work space. This could help run the script better.
clc, clear, close all
a = [105 97 245 163 207 134 218 199 160 196 221 154 228 131 180 178 157 151,...,
175 201 183 153 174 154 190 76 101 142 149 200 186 174 199 115 193 167,...,
171 163 87 176 121 120 181 160 194 184 165 145 160 150 181 168 158 208,...,
133 135 172 171 237 170 180 167 176 158 156 229 158 148 150 118 143 141,...,
110 133 123 146 169 158 135 149];
Mean = mean(a)
Std = std(a)
Max = max(a)
Min = min(a)
Range = range(a)

Skip/Ignore values with maximum difference more than 1 between successive vector from a large array in matlab

Given the following array:
[158 159 159 160 162 163 161 162 162 168 169 163 164 164 165 171 177 178 166 167]
How can I quickly recreate an array which keeps the same order but skips values with the maximum difference more than 1 between successive vector?
The desired result is:
[158 159 159 160 161 162 162 163 164 164 165 166 167]
A= [158 159 159 160 162 163 161 162 162 168 169 163 164 164 165 171 177 178 166 167];
for i=2:length(A)
if i>length(A)
break
end
while abs(A(i)-A(i-1))>1
A(i) = [];
end
end
The starting length(A) is 20 but it will be shorten. Hence, I have to check the condition and break the loop if needed.
I use while instead of if to check whether the successive element. For example, I check the difference between 160 and 162, then 162 was deleted. Then I need to keep check the difference between 160 and 163.
This is a recursive solution:
a= [158 159 159 160 162 163 161 162 162 168 169 163 164 164 165 171 177 178 166 167];
ii=1;
while true
l= length(a);
csa = circshift(a,1);
csa(1)=NaN;
a( a - csa >1 ) = [];
if l == length(a)
break;
end
end
disp(a)
a is the input array modified by the algorithm.
If you run it, the result is:
a = [158 159 159 160 161 162 162 163 164 164 165 166 167]
as desired.
You can use diff along with logical indexing:
a = [158 159 159 160 162 163 161 162 162 168 169 163 164 164 165 171 177 178 166 167];
test = [false diff(a)>1];
while any(test)
a = a(~test);
test = [false diff(a)>1];
end
Which returns:
a =
158 159 159 160 161 162 162 163 164 164 165 166 167

Save variable content to text file

I have 96 x 96 images, I want to convert each image to pixels and save it to a text file. This is an example of code :
frame = imread('c.jpg');
allpixels = reshape(frame, 96*96, 3)
The Output of the command window is :
a
allpixels =
211 194 176
200 183 163
186 169 143
170 150 123
154 133 104
149 128 99
156 135 106
157 136 109
155 135 110
148 126 102
147 123 97
143 118 88
.....
I want to save only the content of the variable allpixels to a text file. It means the text file should be exactly like this :
211 194 176
200 183 163
186 169 143
170 150 123
154 133 104
149 128 99
156 135 106
157 136 109
155 135 110
148 126 102
147 123 97
143 118 88
I have used :
diary('out.txt');
diary on
But out.txt will contain all the command window output, like this :
a
allpixels =
9216×3 uint8 matrix
211 194 176
200 183 163
186 169 143
170 150 123
154 133 104
149 128 99
156 135 106
157 136 109
155 135 110
148 126 102
147 123 97
143 118 88
How can I do that ?
diary is for activity log of your MATLAB session. To store the specific matrix you can use the following code:
dlmwrite('out.txt', a, 'delimiter', ' ');
See the details here.

How to delete all numbers in a vector which are smaller than any previous number?

I have data set like:
1 14.8759
2 14.083
3 0.735268
4 18.2378
5 17.3748
6 4.07867
7 18.2032
8 15.6929
9 4.03338
10 19.0308
11 17.4139
12 17.4139
13 19.8453
14 4.91288
15 20.6746
16 16.578
17 14.8548
18 23.9831
19 19.0691
20 19.0777
21 3.24368
22 25.6457
23 -5.95598
24 32.3198
25 8.20419
26 22.3266
27 17.4016
28 9.0672
29 24.8722
30 24.8262
31 19.8966
32 34.7338
33 29.8088
34 33.1393
35 28.1402
36 35.6231
37 26.4872
38 3.2392
39 5.73463
40 26.4754
41 33.9667
42 27.3048
43 34.75
44 37.2759
45 15.6929
46 28.9686
47 44.6922
48 37.2799
49 25.699
50 45.4923
51 32.2579
52 25.699
53 29.7885
54 50.4719
55 20.6746
56 30.6061
57 38.0448
58 11.5342
59 52.9365
60 44.7128
61 38.0448
62 44.6621
63 13.1939
64 28.9542
65 46.3637
66 13.1939
67 10.7318
68 31.4318
69 29.7885
70 22.3399
71 29.7885
72 26.4754
73 55.4135
74 48.8326
75 42.2395
76 19.0174
77 7.4035
78 13.1939
79 33.9055
80 14.8935
81 27.3048
82 6.56548
83 64.4474
84 48.7848
85 59.5214
86 31.4915
87 59.5214
88 19.8966
89 57.0318
90 21.5631
91 20.7273
92 66.0889
93 58.6749
94 20.6803
95 52.1244
96 16.5242
97 51.3028
98 10.7037
99 12.3958
100 26.5265
101 30.6061
102 74.2826
103 50.4806
104 12.3958
105 17.354
106 40.5832
107 19.8514
108 63.6089
109 27.3559
110 9.06318
111 11.564
112 39.7561
113 29.8368
114 17.3615
115 19.0241
116 69.3539
117 35.6231
118 38.8777
119 34.7394
120 60.3455
121 25.6969
122 54.5637
123 25.6969
124 79.2023
125 31.4876
126 28.184
127 13.2268
128 34.7394
129 12.3602
130 29.0096
131 47.9604
132 82.4815
133 77.5533
134 14.8935
135 33.9055
136 16.5172
137 41.4113
138 34.7956
139 64.4558
140 29.8368
141 19.0108
142 26.5265
143 36.4452
144 50.4761
145 4.87781
146 83.3041
147 61.9694
148 26.5265
149 1.5427
150 71.8344
151 24.8158
152 94.7328
153 19.8915
154 36.4452
155 32.2504
156 26.5265
157 89.0202
158 29.8347
159 93.9223
160 87.3855
161 4.89738
162 88.1694
163 24.0448
164 51.2987
165 65.2679
166 89.8386
167 33.9055
168 67.7414
169 88.9942
170 19.0174
171 92.2651
172 49.6527
173 18.1971
174 19.0108
175 33.9667
176 92.2611
177 32.2789
178 92.2577
179 4.89738
180 102.898
181 34.7956
182 95.5292
183 28.9542
184 91.451
185 25.6457
186 74.2944
187 25.6516
188 47.1323
189 34.7338
190 94.7081
191 97.9775
192 105.334
193 89.812
194 93.8991
195 88.1756
196 10.7318
197 49.611
198 97.1618
199 2.40369
200 44.7128
201 35.6263
202 42.1795
203 53.7678
204 70.2067
205 28.9542
206 19.0241
207 111.849
208 19.8915
209 95.5218
210 38.8723
211 101.238
212 19.8393
213 92.2651
214 102.053
215 24.8221
216 116.713
217 88.9912
218 88.1756
219 115.102
220 58.6995
221 19.8393
222 27.3171
223 23.1511
224 53.7678
225 99.6138
226 120.79
227 32.2579
228 90.6265
229 38.0448
230 48.8284
231 111.054
232 112.608
233 66.9162
234 100.431
235 63.6317
236 19.8334
237 35.6263
238 17.3615
239 2.39774
240 29.7885
241 71.0225
242 66.9162
243 25.6457
244 128.908
245 12.3602
246 93.8991
247 123.218
248 24.8221
249 33.1393
250 110.194
251 31.4547
252 12.3958
253 92.2611
254 10.7037
255 90.6302
256 96.3458
257 102.053
258 37.2167
259 93.0788
260 19.0108
261 102.063
262 16.5617
263 49.611
264 135.388
265 117.522
266 92.2879
267 118.378
268 116.706
269 24.0448
270 128.941
271 132.182
272 137.009
273 48.7848
274 32.2789
275 137.826
276 137.009
277 117.522
278 54.5904
279 16.5172
280 141.064
281 63.6317
282 27.3559
283 108.587
284 38.8723
285 140.247
286 106.13
287 135.426
288 67.7371
289 19.8915
290 112.652
291 27.3227
292 117.522
and want to ignore/delete any Y value which is smaller than its previous value (and delete its corresponding X too) and put the new data set into a new file so that all resulted Y values would be in increasing order.
Thanks.
Assuming:
data = [1 14.8759
2 14.083
3 0.735268
... ... ];
You could do that:
keep = false(size(data, 1), 1);
largest = -Inf;
for i = 1:size(data, 1)
if data(i,2) > largest
largest = data(i,2);
keep(i) = true;
end
end
newdata = data(keep,:)
Result:
newdata =
1.0000 14.8759
4.0000 18.2378
10.0000 19.0308
13.0000 19.8453
15.0000 20.6746
18.0000 23.9831
22.0000 25.6457
24.0000 32.3198
32.0000 34.7338
36.0000 35.6231
44.0000 37.2759
47.0000 44.6922
50.0000 45.4923
54.0000 50.4719
59.0000 52.9365
73.0000 55.4135
83.0000 64.4474
92.0000 66.0889
102.0000 74.2826
124.0000 79.2023
132.0000 82.4815
146.0000 83.3041
152.0000 94.7328
180.0000 102.8980
192.0000 105.3340
207.0000 111.8490
216.0000 116.7130
226.0000 120.7900
244.0000 128.9080
264.0000 135.3880
272.0000 137.0090
275.0000 137.8260
280.0000 141.0640
If you've lot of data then it will be better to use vectorization. Removing for loops will make it faster.
Let's say 'A' is your second column (data).
A = 5 4 8 8 2 5 5 7 8 8;
Since your first column is just index we can leave it for now (Even if it's not you can copy second column to 'A' and proceed).
B = A - [-inf A(1:end-1)];
Aout = [find(B>=0);A(B>=0)];
If your first column is not just index copy it to say 'C' and change the last line to the following.
Aout = [C(B>=0);A(B>=0)];
Use bsxfun to compare each element with all the others, and from that generate a logical index that selects the desired rows:
result = data(~any(triu(bsxfun(#lt, data(:,2).', data(:,2)))), :);

Working with .PGM files?

I need to do some number crunching in .PGM image files.
I'll use MatLab for that.
Now some files (apparently "P2" type) are plain-text and everything is straightforward because they look like this
P2
256 256
255
203 197 197 186 181 181 182 170 165 161 167 171 169 175 163 154 146
138 146 156 166 161 162 164 166 167 177 175 169 167 171 163 153 161
159 159 145 183 181 148 149 151 149 143 175 172 162 156 168 159 159
...
But some files (apparently "P5" type) are like this
P5
256 256
255
*all kinds of random symbols here*
...
Wikipedia here says that the difference is that the latter uses binary encoding. How should I deal with it? I doubt I can import binary data into MatLab...
Have you tired reading the images using imread?
I = imread( pngFilename );