Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 23rd, 2012, 12:12 AM
skysurf skysurf is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 1 skysurf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 15 sec
Reputation Power: 0
Question Iterating through an xml::simple array using a variable?

Hello,

I am new to Perl and trying to write a Perl program to read in an XML file and generate C code (a pointer array of 246 variables) based on the XML file contents. I was having trouble using a numeric variable for a 'for' loop in the array for XML::Simple and wondering if someone can provide insight on what I am doing wrong. The code looks like this,

use XML::Simple;

use Data:umper;


$xml = new XML::Simple (KeyAttr=>[]);


#read XML files

$conf_data = $xml->XMLin("tempConfigDataFile.xml");



open CFILE, ">ACM_FM.c";

print_global_defs();


sub print_global_defs

{

my @FC_Names = @{ $conf_data->{'SW-SYSTEMS'}->{'SW-SYSTEM'}->{'CONF-SPEC'}->{'CONF-ITEMS'}->{'CONF-ITEM'} };

my $FC_Count = @FC_Names;


for(my $i = 0; $i < $FC_Count; $i++) {

print CFILE "&", @FC_Names['$i']->{'CONF-ITEMS'}->{'CONF-ITEM'}->{'CONF-ITEMS'}->{'CONF-ITEM'}->[0]->{VT}, "}\n";

}

}


The problem is that inside the 'for' loop, the variable $i in @FC_Names['$i] always gets evaluated to 0 and it always returns the same element (first one) from the XML file. Thus the c file gets printed out with the same element (the first one) repetatively instead of all the elements consecutively.

Can anyone tell me how to correctly reference the numeric variable 'i' inside the array FC_Names so that the correct value of i is used as an index insde the array such that the correct element of the XML file is referenced?

I would appreciate any help.

Thanks!

Reply With Quote
  #2  
Old October 23rd, 2012, 04:09 AM
Laurent_R Laurent_R is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2012
Posts: 549 Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Laurent_R User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 5 Days 3 h 10 m 32 sec
Reputation Power: 406
Remove the quote marks around $i in your subscript of the array.

Reply With Quote
  #3  
Old October 23rd, 2012, 05:46 AM
ishnid's Avatar
ishnid ishnid is offline
kill 9, $$;
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Sep 2001
Location: Shanghai, An tSín
Posts: 6,894 ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level)ishnid User rank is General 44th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 2 Weeks 1 Day 22 h 49 m 47 sec
Reputation Power: 3885
To expand on the above answer: using single quotes means that your variable isn't interpolated (i.e. it's not replaced with its value). Instead, you're literally using the string '$i' as an array index. That will evaluate to 0 if used as a numeric value.

This is a good example of why you should always use warnings in your programs. If you had, you'd have seen something like this:
Code:
Argument "$i" isn't numeric in array element at test.pl line 18.
Comments on this post
Axweildr agrees!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Iterating through an xml::simple array using a variable?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap