Help with a hash

H

HB

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
 
R

Rainer Weikusat

[...]
./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.
 
J

Jim Gibson

HB said:
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.
 
R

Rainer Weikusat

[...]
- 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.
 
R

Rainer Weikusat

Ben Morrow said:
Quoth Rainer Weikusat said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top