LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-12-2008, 06:38 AM   #1
gaynut
LQ Newbie
 
Registered: Jan 2008
Posts: 27

Rep: Reputation: 15
how do i print the elements of this PERL datastructure


Hi,
Can somebody tell how to print the elements of the below data structure.

@arr= (
"List1"=>
{India=>"Delhi",Srilanka=>"Columbo"},
"List2"=>
{Pakistan=>"islamabad",Afghanisthan=>"kabul"},
);



Thanks,
~gaynut
 
Old 09-12-2008, 07:24 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Try data dumper: http://perldoc.perl.org/Data/Dumper.html
 
Old 09-14-2008, 07:39 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
OR 2 different ways:
Code:
#!/usr/bin/perl -w
use Data::Dumper;
use strict;             # Enforce declarations

my (
    @arr, $elem1, $key1
    );

@arr= (
"List1",
{India=>"Delhi",Srilanka=>"Columbo"},
"List2",
{Pakistan=>"islamabad",Afghanisthan=>"kabul"},
);

print Dumper(@arr);


for $elem1 (@arr)
{
    if( ref($elem1) eq "HASH" )
    {
        for $key1 (keys %$elem1 )
        {
            print "$key1 $elem1->{$key1}\n";
        }
    }
    else
    {
        print "$elem1\n";
    }
}
 
Old 09-18-2008, 09:41 AM   #4
gaynut
LQ Newbie
 
Registered: Jan 2008
Posts: 27

Original Poster
Rep: Reputation: 15
Thanks chrism01.
i came to know about dumper module thro' this.
Btw, sorry for the delayed reply.
I gotta work on it only today.


Thanks
~ gaynut
 
Old 09-18-2008, 07:25 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
No worries.
If you haven't already, bookmark/read these:
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
trimming perl array elements homey Programming 7 02-17-2008 03:48 PM
print array elements in one line bharatbsharma Programming 1 10-29-2007 08:58 AM
syntax for gdb to print elements of 2D array? johnpaulodonnell Programming 4 03-26-2007 07:24 AM
Deleting elements from array in perl with splice signalno9 Programming 2 08-16-2005 10:57 PM
perl - get number of elements in an array AM1SHFURN1TURE Programming 3 03-07-2005 03:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration