> Hello all,
> 
> I want to make a function to list all jobs name from a printer.
> The format of the jobs name is like d00001-001, d00002-001.
> To list all the jobs and get the correct job name, i use the lpstat
command, that returns me lines
> like this one: 
> _DATA_
> HP4100V-4               rodza          2597888   Seg 21 Jun 2004
12:56:56 BRT
> _DATA_
> 
> in the first camp i got the printer name (HP4100V) followed by the job
numer (separated by -), the
> owner of the job, the job length in bytes and the started job data.
> 
> What i mean here is to use a hash with 2 camps: 'name' and 'jobs'. In
the 'name' i put the printer
> name. In the 'jobs' i put a hash reference that got the job name like
his key associated with the
> owner job name.
> 
> well, i write this code that are not working::
> 
> _BEGIN_
> #!/usr/bin/perl
> use warnings;
> use strict;

Excellent start...

> 
> sub lst_job{ # this sub list all the jobs name from a printer
>         my ($printer) = @_;
>         my @lpstat = `lpstat -P $printer`;
>         my %jobs;
>         #here i assembly the job name (like d00001-01) 
>         for(my $i=0; $i < @lpstat; $i++){
>                 $lpstat[$i] =~ /\w+\-(\d+)\s+(\w+)\s+/;
>                 $jobs{sprintf("d%05d-001",$1)} = $2;
>         } 

This will work and is very Cesque, to make it more Perlish you might
consider a foreach loop,

foreach my $job (@lpstat) {
  $job =~ /...../;
   blah blah blah...
}

For me it is a bit more readable, but to each their own.

>         my %printer = ( name => $printer,
>                         jobs => \%jobs,
>                 );
>        return %printer;
> }
> 
> my %printers = lst_job("HP4100V");
> print "The printers: ".$printers{name}." got this job(s):\n";
> foreach my $keys (keys $printers{jobs}){

In the above line, as the error suggests 'keys' must take a hash as its
argument, $printers{jobs} is a scalar, so essentially you need to
dereference its value back into a hash, ending up with,

foreach my $keys (keys %{ $printers{jobs} }) {
...

>         print $printers{jobs}->{$key}."\n";

If you use $keys in the loop, then $key will throw a syntax error with
strict on, one should be plural the other shouldn't, which I was going
to suggest for readability anyways (aka make $key singular).

> };
> _END_
> 
> this is the error i got:
> 
> _ERROR_
> Type of arg 1 to keys must be hash (not hash element) at printer.pm
line 61, near "})"
> _ERROR_
> 
> Can someone help me?
> 

HTH,

http://danconia.org


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to