School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,358 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,924 people online right now. Registration is fast and FREE... Join Now!




Array elements!

 

Array elements!

Sun751

2 Jul, 2009 - 11:54 PM
Post #1

D.I.C Head
**

Joined: 11 Dec, 2008
Posts: 57



Thanked: 1 times
My Contributions
Hi everyone,

In my project, I am using configuration file where array holding lots of hash for example,
CODE

@CFG=( { "CMD" =>"Command 1",
         "DES" => 1,
       },
       { "CMD" =>"Command 2",
         "DES" => 2,
       },
       { "CMD" =>"Command 3",
         "DES" => 3,
       },
       { "CMD" =>"Command 4",
         "DES" => 4,
       },
       { "CMD" =>"Command 5",
         "DES" => 5,
       },
       { "CMD" =>"Command 6",
         "DES" => 6,
       },

);


And in my script I reading config file (above array) and executing it like,
CODE

"$CFG::CFG[0]->{'CMD'}";


And each time my script runs I want to calculate number of an array elements, I know for regular array we can do,
CODE

print scalar @CFG;


But can some one suggest me how can that be done in my scenario, Please!

Cheers



User is offlineProfile CardPM
+Quote Post


dsherohman

RE: Array Elements!

3 Jul, 2009 - 04:32 AM
Post #2

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 184



Thanked: 35 times
My Contributions
Have you tried "print scalar @CFG"?
CODE

#!/usr/bin/perl

use strict;
use warnings;

my @CFG=( { "CMD" =>"Command 1", "DES" => 1, },
          { "CMD" =>"Command 2", "DES" => 2, },
          { "CMD" =>"Command 3", "DES" => 3, },
          { "CMD" =>"Command 4", "DES" => 4, },
          { "CMD" =>"Command 5", "DES" => 5, },
          { "CMD" =>"Command 6", "DES" => 6, },
);

print scalar @CFG, "\n";
runs fine for me (no errors or warnings) and prints "6", as expected. Or was there some other result that you wanted to produce?
User is offlineProfile CardPM
+Quote Post

Sun751

RE: Array Elements!

3 Jul, 2009 - 03:04 PM
Post #3

D.I.C Head
**

Joined: 11 Dec, 2008
Posts: 57



Thanked: 1 times
My Contributions
QUOTE(dsherohman @ 3 Jul, 2009 - 04:32 AM) *

Have you tried "print scalar @CFG"?
CODE

#!/usr/bin/perl

use strict;
use warnings;

my @CFG=( { "CMD" =>"Command 1", "DES" => 1, },
          { "CMD" =>"Command 2", "DES" => 2, },
          { "CMD" =>"Command 3", "DES" => 3, },
          { "CMD" =>"Command 4", "DES" => 4, },
          { "CMD" =>"Command 5", "DES" => 5, },
          { "CMD" =>"Command 6", "DES" => 6, },
);

print scalar @CFG, "\n";
runs fine for me (no errors or warnings) and prints "6", as expected. Or was there some other result that you wanted to produce?


ummm... looks like I am not begin able to visualize u my situation here,

I have configuration file where I have got array @CFG,
And in my script I am reading that array(not using filehandle) and accessing elements of that array using following
command, "$CFG::CFG[0]->{'CMD'}";

As In your script you are getting 6 out of this command
print scalar @CFG, "\n";

I also want to find out the number of elements, But how?
I tried following commands but non-workes,
CODE

print scalar @CFG::CFG
print scalar @CFG, "\n";


Any Suggestion???
User is offlineProfile CardPM
+Quote Post

KevinADC

RE: Array Elements!

3 Jul, 2009 - 10:38 PM
Post #4

D.I.C Regular
Group Icon

Joined: 23 Jan, 2007
Posts: 401



Thanked: 25 times
Dream Kudos: 50
My Contributions
How are you reading/opening the config file and reading it into your perl script?
User is offlineProfile CardPM
+Quote Post

dsherohman

RE: Array Elements!

4 Jul, 2009 - 01:46 AM
Post #5

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 184



Thanked: 35 times
My Contributions
QUOTE(Sun751 @ 3 Jul, 2009 - 11:04 PM) *

I have configuration file where I have got array @CFG,

And in my script I am reading that array(not using filehandle) and accessing elements of that array using following
command, "$CFG::CFG[0]->{'CMD'}";

As In your script you are getting 6 out of this command
print scalar @CFG, "\n";

I also want to find out the number of elements, But how?
I tried following commands but non-workes,
CODE

print scalar @CFG::CFG
print scalar @CFG, "\n";


I guess I'm still not following you, because, if you can access $CFG::CFG[0], then you can access @CFG::CFG and "scalar @CFG::CFG" should tell you how many items are in that array.

Can you post a minimal (but complete/runnable) failing test case, including both a file demonstrating where @CFG is defined and one demonstrating how you're accessing it, along with what your desired output from the test case would be?
User is offlineProfile CardPM
+Quote Post

Sun751

RE: Array Elements!

5 Jul, 2009 - 06:25 PM
Post #6

D.I.C Head
**

Joined: 11 Dec, 2008
Posts: 57



Thanked: 1 times
My Contributions
QUOTE(dsherohman @ 4 Jul, 2009 - 01:46 AM) *

QUOTE(Sun751 @ 3 Jul, 2009 - 11:04 PM) *

I have configuration file where I have got array @CFG,

And in my script I am reading that array(not using filehandle) and accessing elements of that array using following
command, "$CFG::CFG[0]->{'CMD'}";

As In your script you are getting 6 out of this command
print scalar @CFG, "\n";

I also want to find out the number of elements, But how?
I tried following commands but non-workes,
CODE

print scalar @CFG::CFG
print scalar @CFG, "\n";


I guess I'm still not following you, because, if you can access $CFG::CFG[0], then you can access @CFG::CFG and "scalar @CFG::CFG" should tell you how many items are in that array.

Can you post a minimal (but complete/runnable) failing test case, including both a file demonstrating where @CFG is defined and one demonstrating how you're accessing it, along with what your desired output from the test case would be?


Hi dsherohman,
I got it working, there was some scripting error with my configuration file,
Any way thanks!!!

Cheers
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 07:37PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month