Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Hash dereferencing and printing

by Anonymous Monk
on Jun 03, 2002 at 15:14 UTC ( #171255=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a program which returns backup data. It currently uses only one function of a module an I would like to modify it to use others.

Truncated contents of %images:

172 'galahad_1023056445' 173 HASH(0x5cdf98) 'backup_id' => 'galahad_1023056445' 'backup_status' => 0 'backup_time' => 1023056445 'bi_full_time' => 0 'class' => 'MII-AFS-Filesystem' 'class_type' => 'Standard (0)' 'client' => 'galahad' 'id' => 'CC0981'

Truncated contents of %jobs:

68 212844 69 HASH(0x7eff68) 'class' => 'NT-Development' 'classtype' => 13 'client' => 'ahrdev' 'compression' => 0 'elapsed' => 0000037093 'ended' => 1022923736 'filelistcount' => 1 'files' => 'ALL_LOCAL_DRIVES' 'fileslastwritten' => 0 'group' => '' 'jobid' => 212844 'jobpid' => 17667 'jobtype' => 'immediate' 'kbytes' => 295857

 %images and %jobs are populated like this:

my %images = bpimagelist('$record->{client}'); my %jobs = bpdbjobs();

Subroutine which produces desired output:

sub post2Web { my $jRef = shift(); my $infoformat = "%-30s %-40s\n"; ### Base directory of Web database my $base = '/usr/openv/netbackup/jobs/db'; ### Job ID index directory my $jidbase = '/usr/openv/netbackup/jobs/byJobID'; ### make today's client directory if ! exist my $bkup_date = strftime "%Y%m%d", localtime($jRef->{started}); my $wkdir = "$bkup_date/TEST/$jRef->{client}"; mkpath("$base/$wkdir", 0, 0755) unless -d "$base/$wkdir"; ### make directory to hold class/schedule/jobid info my $csdir = "$jRef->{class}/$jRef->{schedule}/$jRef->{jobid}"; mkpath("$base/$wkdir/$csdir", 0, 0755) unless -d "$base/$wkdir/$csdir" +; symlink "../db/$wkdir/$csdir", "$jidbase/$jRef->{jobid}"|| die "$!"; open(INFO, ">$base/$wkdir/$csdir/info") || die "Unable to open/write + $base/$wkdir/csdir/info, $!"; printf INFO $infoformat, "jobid", $jRef->{jobid}; printf INFO $infoformat, "jobtype", $jRef->{jobtype}; printf INFO $infoformat, "state", $jRef->{state}; printf INFO $infoformat, "status", $jRef->{status}; printf INFO $infoformat, "client", $jRef->{client}; printf INFO $infoformat, "server", $jRef->{server}; printf INFO $infoformat, "stunit", $jRef->{stunit}; printf INFO $infoformat, "started", $jRef->{started}; printf INFO $infoformat, "elapsed", $jRef->{elapsed}; printf INFO $infoformat, "ended", $jRef->{ended}; printf INFO $infoformat, "files", $jRef->{files}; printf INFO $infoformat, "Mbytes", $jRef->{Kbytes}; printf INFO $infoformat, "kbytes last written", close INFO; } </CODE<p> What I want to do is incorporate pertinent values from <CODE> %image
in the output.
This separate piece of code will give me the desired output, but I just don't know how to combine the two.
#!/usr/local/bin/perl -w use strict; use NBUX; my $record; my %images = bpimagelist('$record->{client}'); foreach my $key (sort keys %images) { prtID($images{$key}); } sub prtID { my $ref = shift; print "client = $ref->{client}\n"; print "\tid= $ref->{id}\n"; }

Sorry if this is a little verbose; please feel free to say so.

Edit kudra, 2002-06-06 Added readmore

Replies are listed 'Best First'.
Re: Hash dereferencing and printing
by talexb (Chancellor) on Jun 03, 2002 at 16:31 UTC
    I'm guessing that what you want to do is pass both hashes into the subroutine. I can suggest two options:
    my %images = bpimagelist('$record->{client}'); my %jobs = bpdbjobs();
    1. Pass by reference

      post2web ( \%jobs, \%images ); ... sub post2Web { my ( $jRef, $iRef ) = @_; ...
    2. Pass using argument hash

      post2web ( { jobs => %jobs, images => %images } ); ... sub post2web ( my ( $Args ) = @_; my $jRef = %{ $Args->[ jobs ] }; my $iRef = %{ $Args->[ images ] }; ...
    Certainly of the two I perfer the second alternative -- about as simple to setup, and further expansion is a piece of cake, complete with backward compatibility.

    --t. alex

    "Nyahhh (munch, munch) What's up, Doc?" --Bugs Bunny

    ps Code not tested. :)

Re: Hash dereferencing and printing
by Russ (Deacon) on Jun 03, 2002 at 16:30 UTC
    If I understand the issue, you just need to pass two hashes to post2Web:
    sub post2Web { my ($jRef, $iRef) = @_; # Now just use $jRef, and $iRef printf INFO $infoformat, 'jobid', $jRef->{jobid}; printf INFO $infoformat, 'backup_time', $iRef->{backup_time}; } # Call it with references to your hashes as you would expect: post2Web(\%jobs, \%images);

    Brainbench 'Most Valuable Professional' for Perl

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://171255]
Approved by vek
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2022-07-02 03:09 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (102 votes). Check out past polls.