Re: How to Print Return Data from Subroutine
by toolic (Bishop) on Aug 24, 2008 at 01:57 UTC
|
After changing your parentheses to curlies in your sub, and adding a,b,c variables, then assigning the output of your sub to a scalar variable (a reference), I just print the results using Data::Dumper. I also print what is returned by the ref function:
use strict;
use warnings;
use Data::Dumper;
my $ref = parse();
print Dumper($ref);
print ref $ref, "\n";
sub parse {
my @a = 0..2;
my %b = (foo => 7);
my $c = 55;
return {
a => \@a,
b => \%b,
c => $c
}
}
__END__
$VAR1 = {
'c' => 55,
'a' => [
0,
1,
2
],
'b' => {
'foo' => 7
}
};
HASH
| [reply] [d/l] |
|
|
Thank you so much. If you can be a little bit more patient with me, how do I print them in the following format without using data dumper?
print $ref->{c}; # This gave me 55 which is what I want.
foreach my $i ($ref->@a) {
print $i; # I want to see the content of the array, this
+ gave me an error.
}
foreach my $i ($ref->%b} {
print $i; # I want to see the content of the hash.
}
| [reply] [d/l] |
|
|
foreach my $i ( @{$ref}{@a} ) {
print $i;
}
Or maybe it's simpler than that:
foreach my $i ( @a ) {
print $i;
}
| [reply] [d/l] [select] |
|
|
foreach my $i ( %{ $ref->{b} } ) {
print $i; # I want to see the content of the hash.
}
(It will print keys and values by turns.) | [reply] [d/l] [select] |
Re: How to Print Return Data from Subroutine
by Burak (Chaplain) on Aug 24, 2008 at 01:55 UTC
|
use Data::Dumper;
print Dumper \%hash;
| [reply] [d/l] |
Re: How to Print Return Data from Subroutine
by ww (Archbishop) on Aug 24, 2008 at 02:13 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %wtf;
my $string = "test me";
my %hash = parse($string);
print "Dumping from main:\n";
print Dumper %wtf;
print "------\n\n";
print "dumping hash\n";
print Dumper %hash;
print "__________\n\n";
print "\n printing hash:\n";
print Dumper(%hash);
print ref %hash, "\n";
sub parse {
my @a = split (/ /,"test me");
my %b = (
"one" => "1",
"two" => "2",
"three" => "3",
);
my $c = "c";
%wtf = (
aaa => \@a,
bbbb => \%b,
ccccc => $c,
);
return %wtf;
}
output:
F:\_wo\pl_test>perl 706464.pl
Dumping from main:
$VAR1 = 'ccccc';
$VAR2 = 'c';
$VAR3 = 'bbbb';
$VAR4 = {
'three' => '3',
'one' => '1',
'two' => '2'
};
$VAR5 = 'aaa';
$VAR6 = [
'test',
'me'
];
------
dumping hash
$VAR1 = 'ccccc';
$VAR2 = 'c';
$VAR3 = 'bbbb';
$VAR4 = {
'three' => '3',
'one' => '1',
'two' => '2'
};
$VAR5 = 'aaa';
$VAR6 = [
'test',
'me'
];
__________
printing hash:
$VAR1 = 'ccccc';
$VAR2 = 'c';
$VAR3 = 'bbbb';
$VAR4 = {
'three' => '3',
'one' => '1',
'two' => '2'
};
$VAR5 = 'aaa';
$VAR6 = [
'test',
'me'
];
| [reply] [d/l] [select] |
Re: How to Print Return Data from Subroutine
by GrandFather (Saint) on Aug 24, 2008 at 10:06 UTC
|
The key to understanding what manner of reply you want is to know why you want to print the contents of the hash. If you simply want debugging output then the Dumper solution is the best answer. If however you want to use the printed hash contents as report output or in UI then you need either to know what the structure of the data is and handle it explicitly, or you need to parse the data in a similar fashion to Dumper.
There are two keys to Dumper type processing: recursion and ref. There is also a nasty gotcha: if there are circular references in the structure things will go pear shaped very fast!
Perl reduces RSI - it saves typing
| [reply] |
|
|
Thank you so much for helping me out, Monks!
| [reply] |
Re: How to Print Return Data from Subroutine
by logie17 (Friar) on Aug 24, 2008 at 05:26 UTC
|
You're returning an anonymous hash. So you could just use Data::Dumper to get a list of your results.
use Data::Dumper
my $hash = parser();
print Dumper($hash);
If you want to print a particular key in the hash you'll need to use this syntax:
print $hash->{'key'};
Thanks,
s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;
| [reply] [d/l] [select] |
Re: How to Print Return Data from Subroutine
by ww (Archbishop) on Aug 24, 2008 at 02:18 UTC
|
Like toolic's (except that he beat me to most of it) but I figured your $string was not intended to be a no-op:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %wtf;
my $string = "test me";
my %hash = parse($string);
print "Dumping from main:\n";
print Dumper %wtf;
print "------\n\n";
print "dumping hash\n";
print Dumper %hash;
print "__________\n\n";
print "\n printing hash:\n";
print Dumper(%hash);
print ref %hash, "\n";
sub parse {
my @a = split (/ /,"test me");
my %b = (
"one" => "1",
"two" => "2",
"three" => "3",
);
my $c = "c";
%wtf = (
aaa => \@a,
bbbb => \%b,
ccccc => $c,
);
return %wtf;
}
Output:
perl 706464.pl
Dumping from main:
$VAR1 = 'ccccc';
$VAR2 = 'c';
$VAR3 = 'bbbb';
$VAR4 = {
'three' => '3',
'one' => '1',
'two' => '2'
};
$VAR5 = 'aaa';
$VAR6 = [
'test',
'me'
];
------
dumping hash
$VAR1 = 'ccccc';
$VAR2 = 'c';
$VAR3 = 'bbbb';
$VAR4 = {
'three' => '3',
'one' => '1',
'two' => '2'
};
$VAR5 = 'aaa';
$VAR6 = [
'test',
'me'
];
__________
printing hash:
$VAR1 = 'ccccc';
$VAR2 = 'c';
$VAR3 = 'bbbb';
$VAR4 = {
'three' => '3',
'one' => '1',
'two' => '2'
};
$VAR5 = 'aaa';
$VAR6 = [
'test',
'me'
];
| [reply] [d/l] [select] |
|
|
Thank you all for giving quick replies. I noticed all of you are using data dumper to print the output. But I wonder if there is another way to print the output eg. using foreach for the hashes and array as I want to further manipulate the content of the array / hashes.
| [reply] |
|
|
I wonder if there is another way to print the output
Another way to extract the key/value pairs from a hashref:
use strict;
use warnings;
my %h = (foo => 1, bar => 2, foobar => 3);
my $hashref = \%h;
print $hashref, "\n"; # prints HASH(0x.......);
for (keys(%$hashref)) {
print "$_ => $hashref->{$_}\n";
}
Cheers, Rob | [reply] [d/l] |
|
|
|
|