On Friday 02 July 2004 12:05, Rod Za wrote: > > Hello all, Hello,
> 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; > > 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++){ You can just use a foreach loop here: foreach ( @lpstat ) { > $lpstat[$i] =~ /\w+\-(\d+)\s+(\w+)\s+/; > $jobs{sprintf("d%05d-001",$1)} = $2; You shouldn't use the numeric variables unless the regular expression matched successfully and you should anchor the regexp so it doesn't match the wrong thing. $jobs{ sprintf 'd%05d-001', $1 } = $2 if /^\w+-(\d+)\s+(\w+)\s+/; > } > my %printer = ( name => $printer, > jobs => \%jobs, > ); > return %printer; You don't need to create a hash here, just return the list. return name => $printer, jobs => \%jobs; > } You could write it like this: :-) sub lst_job { my $printer = shift; my $flag; return name => $printer, jobs => { map +( $flag = !$flag ) ? sprintf( 'd%05d-001', $_ ) : $_, map /^\w+-(\d+)\s+(\w+)\s+/, `lpstat -P $printer` } } > my %printers = lst_job("HP4100V"); > print "The printers: ".$printers{name}." got this job(s):\n"; Perl will interpolate scalars in strings. print "The printers: $printers{name} got this job(s):\n"; > foreach my $keys (keys $printers{jobs}){ You need to dereference the hash reference in $printers{jobs}. for my $key ( keys %{ $printers{ jobs } } ) { > print $printers{jobs}->{$key}."\n"; The '->' is optional in this instance. print "$printers{jobs}{$key}\n"; > }; > _END_ John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>