473,472 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to traverse through a array reference

1 New Member
Hi

I am having problems going through the hash reference and outputting the result.

Can someone tell me how to traverse thorugh the output of "p4 info" using Perl.
p4 info is a command in perforce.

Is there any way I can increment an address and look what is stored inside till I hit EOF just like C . The reason is that this array could be an array of hashes or array of arrays or any mixed combination.

Any help is greatly appreciated ?

Expand|Select|Wrap|Line Numbers
  1. sub run_info()
  2.   {
  3.      my $key;
  4.      my $value;
  5.      my $p4_info;
  6.      my $val;
  7.      $p4_info = $p4->Run( "info" );
  8.  
  9. ## p4_info is a reference to an array but I dont know what is inside that array.
  10. ##    p4_info is ARRAY(0x226f8c) 
  11.  
  12.      print ("\n p4_info is $p4_info \n");
  13.     #my $ref = \@p4_info;
  14.     foreach  $key ( sort keys %{$p4_info})
  15.    {
  16.   print( $key, "->  \n" );
  17.          #print @{$p4_info->{"clientAddress"};
  18.         }
  19.   }
Thanks
Sunny
Jan 5 '09 #1
2 7938
KevinADC
4,059 Recognized Expert Specialist
if $p4_info is an array:

Expand|Select|Wrap|Line Numbers
  1. foreach my $line (@{$p4_info}) {
  2.    print "$line\n";
  3. }
if $p4_info is a hash:

Expand|Select|Wrap|Line Numbers
  1. foreach my $key (keys %{$p4_info}) {
  2.    print "$key = $pf_info->{$key}\n";
  3. }
if you need to check what type of a reference something is use the ref() function. A simple example:

Expand|Select|Wrap|Line Numbers
  1. my $ref = ref($p4_info);
  2. if ($ref eq 'ARRAY') {
  3.     its an array
  4. }
  5. elsif ($ref eq 'HASH') {
  6.    its a hash
  7. }
  8. else {
  9.    its something else
Jan 6 '09 #2
eWish
971 Recognized Expert Contributor
If you want to see what is inside an array or hash you can use Data::Dumper

If you want to loop through an array reference you can use the following.

Expand|Select|Wrap|Line Numbers
  1. my @my_array = qw/1 2 3 4 5 6 7 8 9 10/;
  2.  
  3. my $array_ref = \@my_array;
  4.  
  5. #Data::Dumper apporach
  6. print Dumper(\$array_ref), "\n";
  7.  
  8. #Print each elemnt of the array reference joined with a newline
  9. print join "\n", @{$array_ref};
  10.  
  11. #Loop through each element of the array reference
  12. for (@{$array_ref}) {
  13.     print $_, "\n";
  14. }
--Kevin
Jan 6 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: RoundKill | last post by:
My problem is that I can't find a way of referencing files and includes statically. I have to change each page reference so that when it is referenced it references the correct document. As I want...
3
by: john | last post by:
I am using ASP.NET and I have a datagrid. One of the columns in my grid is all checkboxes. When the user clicks on a certain button on the page, which is not in the grid, I want to be able to...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
3
by: R. Rajesh Jeba Anbiah | last post by:
Unfortunately, I couldn't find any way to traverse the object array in reverse order. I'd thought there must be a way to do it with for..in loop, but couldn't find anything yet. Could someone...
1
by: Michael D. Reed | last post by:
I have two tables in an Access database with a Many-to-Many relationship, there is a connection table. The tables are , , and the connection table is this is a standard Many-to-Many relationship...
4
by: Gary Wessle | last post by:
hi I did not find any way to traverse a given directory in c++ till I found boost, not sure if there many boost(ers) out here. but this is given me some error I cannot locate a fix. thanks ...
2
by: rvimalram | last post by:
Hi all, I have a created a multidimesional array,what i need is to traverse that array and return the value of the array in the particular page the array is $one=array("one" =>"Text1");...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
7
by: sumitshining | last post by:
if there r around 100000 elements in an array then how to count no of elements with exactly two repetitions in a single traverse????
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.