Sponsored Content
Full Discussion: pointer in perl
Top Forums Shell Programming and Scripting pointer in perl Post 302434839 by pludi on Monday 5th of July 2010 11:37:53 AM
Old 07-05-2010
Perl has no pointers, but references. And you can traverse a hash of hashes (as in your example) the same way as a regular hash:
Code:
foreach my $name ( sort keys %{$config} ) {
    foreach my $option (sort keys %{$config->{$name}} ) {
        print "$name -> $option -> ", $config->{$name}->{$option}, "\n";
    }
}


Last edited by pludi; 07-05-2010 at 01:03 PM..
 

10 More Discussions You Might Find Interesting

1. Programming

pointer

void main() { int a={1,2,3,4,5,6,7,8,9,10}; int *p=a; int *q=&a; cout<<q-p+1<<endl; } The output is 10, how? if we give cout<<q it will print the address, value won't print.... if we give cout<<p it will print the address, value won't print.... p has the base addr; q... (1 Reply)
Discussion started by: sarwan
1 Replies

2. Programming

file pointer

hi all: bankpro again, i want to open a file and close it after an hour (i.e open and close the file ptr) In the mean time i will be executing my jobs using the shell (CommandInterpreter). plz help... :cool: (3 Replies)
Discussion started by: bankpro
3 Replies

3. Programming

file pointer

what is the difference between a file pointer and a file descriptor. (1 Reply)
Discussion started by: bankpro
1 Replies

4. Programming

Regarding char Pointer

Hi, char *s="yamaha"; cout<<s<<endl; int *p; int i=10; p=&i; cout<<p<<endl; 1) For the 1st "cout" we will get "yamaha" as output. That is we are getting "content of the address" for cout<<s. 2) But for integer "cout<<p" we are getting the "address only". Please clarify how we are... (2 Replies)
Discussion started by: sweta
2 Replies

5. Programming

far pointer

what is far pointer in C (1 Reply)
Discussion started by: useless79
1 Replies

6. Shell Programming and Scripting

Perl how to move pointer to previous line in a txt file?

I have a text file that has blocks of text. Each block starts with ### and ends with End_###. I wrote a perl script to search a string from line 2 (ignore any line starts with ###) of each block if matched, need to print that whole block. According to the input file in below, it will print... (5 Replies)
Discussion started by: tqlam
5 Replies

7. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

8. Programming

C dynamic pointer

Hi, Can anyone tell me how i can declare and allocate dynamically an array of pointers to structured type?? Is declaration something like this:? struct_name ** array; (1 Reply)
Discussion started by: littleboyblu
1 Replies

9. Programming

Pointer Arithmetic In C

I have a fundamental question on C pointer arithmetry.. Suppose i have a c string pointer already pointing to a valid location, Can I just do a charptr = charptr +1; to get to the next location, irregardless if my program is 32 or 64 bits? or should i do it this way: charptr =... (1 Reply)
Discussion started by: Leion
1 Replies

10. Programming

Pointer to pointers

Hi guys, I'm trying to understand pointers in C and made a simple example and I've problems with It. Can someone help? #include <stdio.h> #include <stdlib.h> #include <assert.h> int f1(char **str_); int main(int argc, char **argv) { char *str = NULL; f1(&str); ... (3 Replies)
Discussion started by: pharaoh
3 Replies
Data::Transformer(3pm)					User Contributed Perl Documentation				    Data::Transformer(3pm)

NAME
Data::Transformer - Traverse a data structure, altering it in place SYNOPSIS
use Data::Transformer; # A: SIMPLE USAGE: # trim extra whitespace from normal strings inside %data. my $trim = sub { local($_)=shift; $$_ =~ s/^s*//; $$_ =~ s/s*$//; }; my $t = Data::Transformer->new(normal=>$trim); $t->traverse(\%data); # B: MORE COMPLEX USAGE: # (a) uppercase all keys in all hashes contained in $data # and (b) convert any arrays to hashes: my $uc_hash = sub { my $h = shift; my @keys = keys %h; foreach (@keys) { my $uc = uc($_); if ($uc ne $_ && !exists($h->{$uc})) { $h->{$uc} = $h->{$_}; delete $h->{$_}; } elsif ($uc ne $_) { die "Bad key $_: '$uc' exists already"; } } }; my $ar_conv = sub { my %h = @{$_[0]}; return sub { \%h }; }; my $t = Data::Transformer->new( hash => $uc_hash, array => $ar_conv, node_limit => 500_000 ); eval { $t->traverse($data) }; warn "Could not complete transformation: $@" if $@; # C: NON-DESTRUCTIVE TRAVERSAL # You don't actually have to change anything... my $size = 0; my $t = Data::Transformer->new( normal => sub { $size+=length(${ $_[0] }) }, hash => sub { $size+=length($_) for keys %{ $_[0] } }, ); my $nodes = $t->tranverse(\%data); print "Number of nodes: $nodes "; print "Size of keys + values: $size "; # D: OBJECTS INSIDE A DATA STRUCTURE # Affect objects by using the class name as a key: my $t = Data::Transformer->new( 'My::Class' => sub { shift->set_foo('bar') } ); DESCRIPTION
Data type callbacks The basic idea is that you provide a callback subroutine for each type of data that you wish to affect or collect information from. The constructor, "new()", expects a hash with at least one of the following keys: * normal : used for normal, non-reference data * array : used for array references * hash : used for hash references * code : used for anonymous subroutines (coderefs) * scalar : used for scalar references * glob : used for globs (such as filehandle holders) The value in each case is a coderef representing the callback for the data type in question. The array and hash types are special in that they are traversed into. It is possible to affect objects inside the data structure by specifying a callback keyed to the name of the class they belong to. They are not automatically recursed into, however, even if they happen to be blessed hash or array references. Similarly, a scalar reference is not automatically traversed into, even if it may contain a reference to an arrayref or a hashref. To make the module traverse into scalar references, you need to return a coderef encapsulating a different data type in the scalar handler, thus changing them (and prompting a reiteration over that data point). This applies to objects as well. Additional option for the constructor node_limit: If an integer value for this is specified, it overrides the default node processing limit of 2**16. This cannot be set higher than 2**20-1. traverse() The traverse() method returns the number of nodes processed. This may be different from both the number of nodes in the actual data structure and the number of nodes after the transformation, for the following reasons: * Reiteration into a particular node may have occurred, which increments the node count. * Blessed references (objects) will not normally be iterated into, but are merely treated as leaves in the data structure. * The processing code passed to the constructor may well affect the number of nodes. Note on data type changes If you want to change a data type (for instance replace an array by a hash as in example B, above) you have to return a coderef from the callback for the original data type. This coderef encapsulates the replacement data for the node in question. After the node has thus been replaced, it is re-evaluated to apply any transformations you may have defined for the new data type. Be careful of potential infinite loops when doing this with more than one data type at a time or when replacing coderefs with other coderefs. Also, because of reiteration, complex changes of large data structures may require setting the node processing limit higher than the default. Note on circular references Data structures containing circular references should not cause problems. Data::Transformer will skip any node containing a reference which has already been processed. CAVEATS
It is not feasible to use this module for very large data structures. Accordingly, there is a hard node processing boundary of 2**20-1 (about 1 million); attempting to set the limit higher results in an immediate, fatal error. For the vast majority of cases, however, the default limit of 2**16 (about 65 thousand) should be more than enough. SEE ALSO
I am aware of two modules doing similar things. Check them out if this one does not fit your needs: o Data::Rmap by Brad Bowman o Data::Walk by Guido Flohr AUTHOR
Baldur Kristinsson <bk@mbl.is>, 2006 Copyright (c) 2006 Baldur Kristinsson. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.10.1 2006-02-05 Data::Transformer(3pm)
All times are GMT -4. The time now is 04:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy