Help with a hash

Discussion in 'Perl Misc' started by HB, Apr 11, 2012.

  1. HB

    HB Guest

    Hi All,

    I'm looking to create a pretty simple script that will create the
    system V run level links for custom chkconfig scripts on Linux. I was
    thinking it will work with 4 arguments as:

    ../script <run levels on> <start order #> <kill order #> </etc/init.d/
    scriptname>

    If I'm thinking correctly, the best way to do this is put ARGV[0] into
    a hash as: 0=off, 1=off, 2=off, 3=on, etc. I would use only the run
    levels to be on in the argument such as:

    ../script 345 x x x

    My question is on the checking of ARGV[0] and how to drop the info
    into a hash. I.E. in the example above:

    - make sure there are only numbers 0-6 used and max of 7
    - if a number is not present it equals 'off'
    - if a number is present it equals 'on'
    - getting data into hash

    I should be fine once I get this part squared away. Is this the best
    way to go about setting this up and if so can someone help me out with
    this hash?

    Thank you very much in advance.

    Best regards,

    Herb
    HB, Apr 11, 2012
    #1
    1. Advertising

  2. HB <> writes:

    [...]

    > ./script 345 x x x
    >
    > My question is on the checking of ARGV[0] and how to drop the info
    > into a hash. I.E. in the example above:
    >
    > - make sure there are only numbers 0-6 used and max of 7
    > - if a number is not present it equals 'off'
    > - if a number is present it equals 'on'
    > - getting data into hash
    >
    > I should be fine once I get this part squared away. Is this the best
    > way to go about setting this up


    Since your keys are the number 0, 1, 2, 3, 4, 5 and 6, I'd prefer to
    use an array for that. I would also simply use 'undef' as
    'off'. Example how this could be done:

    ------------
    my @levels;

    while ($ARGV[0] =~ /(.)/g) {
    $levels[$1] = 1;
    $1 =~ /[0-6]/ or die("invalid run-level $1");
    }

    printf("%d\t%s\n", $_, $levels[$_] ? 'on' : 'off') for (0 .. 6);
    ------------

    NB: This doesn't check if ARGV[0] is empty or not available at all.

    NB^2: Settting $levels[$1] and the check for invalid values have to be
    done in this order to avoid clobbering $1.
    Rainer Weikusat, Apr 11, 2012
    #2
    1. Advertising

  3. HB

    Jim Gibson Guest

    In article
    <>, HB
    <> wrote:

    > Hi All,
    >
    > I'm looking to create a pretty simple script that will create the
    > system V run level links for custom chkconfig scripts on Linux. I was
    > thinking it will work with 4 arguments as:
    >
    > ./script <run levels on> <start order #> <kill order #> </etc/init.d/
    > scriptname>
    >
    > If I'm thinking correctly, the best way to do this is put ARGV[0] into
    > a hash as: 0=off, 1=off, 2=off, 3=on, etc. I would use only the run
    > levels to be on in the argument such as:
    >
    > ./script 345 x x x
    >
    > My question is on the checking of ARGV[0] and how to drop the info
    > into a hash. I.E. in the example above:
    >
    > - make sure there are only numbers 0-6 used and max of 7


    my $levels = $ARGV[0];
    if( $levels !~ /^[0-6]{1,7}$/ ) {
    die("Invalid first argument: \"$levels\"\n");
    }

    > - if a number is not present it equals 'off'
    > - if a number is present it equals 'on'
    > - getting data into hash


    my @levels;
    $levels[$_] = 'off' for 0..6;
    $levels[$_] = 'on' for split(//,$levels);

    All untested.

    --
    Jim Gibson
    Jim Gibson, Apr 11, 2012
    #3
  4. HB <> writes:

    [...]

    > - make sure there are only numbers 0-6 used and max of 7


    I would also omit the 'at most seven' check. That buys nothing except
    'being anal about syntax' and 343536 can just be treated as 3456
    instead.
    Rainer Weikusat, Apr 11, 2012
    #4
  5. Ben Morrow <> writes:
    > Quoth Rainer Weikusat <>:
    >>
    >> my @levels;
    >>
    >> while ($ARGV[0] =~ /(.)/g) {
    >> $levels[$1] = 1;
    >> $1 =~ /[0-6]/ or die("invalid run-level $1");
    >> }
    >>
    >> printf("%d\t%s\n", $_, $levels[$_] ? 'on' : 'off') for (0 .. 6);
    >> ------------
    >>
    >> NB: This doesn't check if ARGV[0] is empty or not available at all.
    >>
    >> NB^2: Settting $levels[$1] and the check for invalid values have to be
    >> done in this order to avoid clobbering $1.

    >
    > So use an ordinary variable instead:
    >
    > while (my ($l) = $ARGV[0] =~ /./g) {
    > $l =~ /[0-6]/ or die ...;
    > $levels[$l] = 1;
    > }


    This looks like an additional complication that buys exactly nothing.
    Rainer Weikusat, Apr 12, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Red Orchid
    Replies:
    3
    Views:
    1,024
  2. Pieter Claassen
    Replies:
    1
    Views:
    1,093
    CBFalconer
    Aug 4, 2004
  3. Bo Peng
    Replies:
    4
    Views:
    780
  4. rp
    Replies:
    1
    Views:
    491
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    595
    David A. Black
    Jul 2, 2008
Loading...

Share This Page