Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

How can I start incrementing an index at 0?

by raj8 (Sexton)
on Sep 02, 2000 at 05:25 UTC ( #30840=categorized question: print w/replies, xml ) Need Help??
Contributed by raj8 on Sep 02, 2000 at 05:25 UTC
Q&A  > 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

Answer: How can I start incrementing an index at 0?
contributed by Adam

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.

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (4)
    As of 2020-09-28 16:13 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (144 votes). Check out past polls.