4

I am new to Perl. I need to define a data structure in Perl that looks like this:

  city 1 -> street 1 - [ name , no of house , senior people ]
            street 2 - [ name , no of house , senior people ]


  city 2 -> street 1 - [ name , no of house , senior people ]
            street 2 - [ name , no of house , senior people ]

How can I acheive this?

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Sam
  • 95
  • 1
  • 9
  • Do you want to create it programmatically or just define those variables like that once off? – Salgar Aug 13 '09 at 09:30
  • 1
    Are you trying to read in this data from a file of some kind? If so, please supply a short example of that file (feel free to remove the real names and addresses, naturally), so that people can see the format you're working with. More generally, you should take a look at `perldoc perlreftut` for a good introductory discussion of how to make and use references and `perldoc perldsc` for a wonderful cookbook of pre-made structures. You can get both via the terminal or online: http://perldoc.perl.org/index-tutorials.html – Telemachus Aug 13 '09 at 09:34
  • Ya, i am reading from database and doing database programming – Sam Aug 13 '09 at 09:39

5 Answers5

5

Here is an another example using a hash reference:

my $data = {
    city1 => {
        street1 => ['name', 'house no', 'senior people'],
        street2 => ['name','house no','senior people'],
    },
    city2 => {
        street1 => etc...
        ...
    }
};

You then can access the data the following way:

$data->{'city1'}{'street1'}[0];

Or:

my @street_data = @{$data->{'city1'}{'street1'}};
print @street_data;
Logan
  • 276
  • 1
  • 5
  • You only need `->`, where it is obvious that you are working against a reference. So this `$data->{'city1'}{'street1'}[0];` would work just as well. – Brad Gilbert Aug 13 '09 at 18:02
4

I found the answer like

my %city ;

 $city{$c_name}{$street} = [ $name , $no_house , $senior];

i can generate in this way

Sam
  • 95
  • 1
  • 9
  • 1
    Do you have this information in a file or spreadsheet or database of some kind? I doubt that you want to use your program purely for data entry, so it would be easier to figure out the structure of the data and then read the records directly into the complex data structure. (Note that you already have one typo - $stret for $street. Hand entering a lot of data is very error prone.) – Telemachus Aug 13 '09 at 09:36
1

The Perl Data Structures Cookbook, perldsc, may be able to help. It has examples showing you how to create common data structures.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
0
my %city ;

If you want to push

push( @{ city{ $c_name } { $street } }, [ $name , $no_house , $senior] );

(0r)

push @{ city{ $c_name } { $street } }, [ $name , $no_house , $senior];
giammin
  • 18,620
  • 8
  • 71
  • 89
0

You may read my short tutorial at this answer. In short you may put reference to hash into the value.

%hash = ( name => 'value' );
%hash_of_hash = ( name => \%hash );
#OR
$hash_of_hash{ name } =  \%hash;


# NOTICE: {} for hash, and [] for array
%hash2 = ( of_hash => { of_array => [1,2,3] } );
#                  ---^          ---^
$hash2{ of_hash }{ of_array }[ 2 ]; # value is '3'
#     ^-- lack of -> because declared by % and ()


# same but with hash reference
# NOTICE: { } when declare
# NOTICE: ->  when access
$hash_ref = { of_hash => { of_array => [1,2,3] } };
#        ---^
$hash_ref->{ of_hash }{ of_array }[ 2 ]; # value is '3'
#     ---^
Community
  • 1
  • 1
Eugen Konkov
  • 22,193
  • 17
  • 108
  • 158