Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How can I start incrementing an index at 0?

by raj8 (Sexton)
on Sep 02, 2000 at 05:25 UTC ( [id://30840]=perlquestion: print w/replies, xml ) Need Help??

raj8 has asked for the wisdom of the Perl Monks concerning the following question: (arrays)

Hello Perl Monks, I am have been attempting to unpack columns and increment the output, however, the increment operator starts at 1. Here is an example output:
drive 0 dirve 1 drive 2 drive 3 I am using this: open(LIST,"list -l|"); while(<LIST>) { ($drive) = unpack '@0 A3',$_; if(/Drive/) { $count++; } system("list -delete -dr -index ${count}"); }; However, when it gets to 3 it says can't delete 4 because the $i++ started at 1 instead of 0.
Any suggestions would help. thanks-- raj8

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How can I start incrementing an index at 0?
by Adam (Vicar) on Sep 02, 2000 at 05:33 UTC
    First of all, the quick answer: declare $count = -1; somewhere earlier.

    Next the long answer. Your code is not good for a variety of reasons, most of which you learn from practice and experience, which if you hang out here you will gain quickly. Try this:

    use strict; # always. my $drive; my $count = -1; open( LIST, "list -l|" ) or die $!; while(<LIST>) { ($drive) = unpack '@0 A3',$_; ++$count if m/Drive/; system("list -delete -dr -index ${count}"); }
    This code can still made better, but that should get you off to a good start.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-18 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found