CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Need some help please

 



osutrisn
Deleted

Jun 6, 2001, 10:19 AM

Post #1 of 3 (443 views)
Need some help please Can't Post

I'm trying to read a file containing values:

1 2 3 4 5 6 7
8 9 10 11 12 13

and trying to increment a variable.....for example - If ( $value >= 0 and $value <= 7 ) {$value0to7++}
but the problem is the values can go up to 1024.......I'd need 128 IF statements to check all of the values.

Could anyone show me how to loop or approach this problem please?




Mortimer
journeyman

Jun 6, 2001, 4:03 PM

Post #2 of 3 (436 views)
Re: Need some help please [In reply to] Can't Post

There's an article about looping here...

http://tlc.perlarchive.com/articles/perl/rl0004.shtml

Cheers,
Dave.
www.dmscripts.com
davemortimer@bigpond.com




rGeoffrey
User / Moderator

Jun 7, 2001, 8:25 AM

Post #3 of 3 (421 views)
Re: Need some help please [In reply to] Can't Post

You don't need any if statements to do this. And rather than have 128 variables of the form $valueXtoX+8 you can have a single hash with upto 128 keys...


Code
#!/usr/local/bin/perl 

use strict;

my $filestring = <<EOF;
1 2 3 62 63 64 65
252 253 254 255 256 520 521 1022 1023
EOF

my @array = split (" ", $filestring);
my %value;

foreach (@array) {
my $base = 8 * int ($_ / 8);
$value{$base . 'to' . ($base + 7)}++;
}

foreach (sort {$a <=> $b} keys %value) {
print "$value{$_} - $_\n";
}

In this case the output is...


Code
3 - 0to7 
2 - 56to63
2 - 64to71
4 - 248to255
1 - 256to263
2 - 520to527
2 - 1016to1023

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives