Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

routine on each value

7 views
Skip to first unread message

Jason Gray

unread,
Dec 18, 2001, 2:18:06 PM12/18/01
to
Ok, most of this I've gotten down but I want to be able to do this in an
easier way for getting the rank and percent. Im going to 100% then want to
stop.

my file that it matches from looks like this, 0 being the value: key|0

my(@ranks) = qw(blah blah blah);

open(FILE, $path) or die "Can't open $path. $!";
while(<FILE>) {
next if /^\s*$/;
chomp;
($key, $value) = split(/\|/, $_);
$match = $key == $ip;
last if $match;
}
close(FILE);

if($match) {
(($rank = @ranks[0]) && ($percent = "1%")) if $value == 1;
(($rank = @ranks[1]) && ($percent = "2%")) if $value == 2;
(($rank = @ranks[2]) && ($percent = "3%")) if $value == 3;
(($rank = @ranks[3]) && ($percent = "4%")) if $value == 4;
(($rank = @ranks[4]) && ($percent = "5%")) if $value == 5;
(($rank = @ranks[5]) && ($percent = "6%")) if $value == 6;
(($rank = @ranks[5]) && ($percent = "7%")) if $value == 7;
(($rank = @ranks[6]) && ($percent = "8%")) if $value == 8;
(($rank = @ranks[7]) && ($percent = "9%")) if $value == 9;
(($rank = @ranks[8]) && ($percent = "10%")) if $value == 10;
}

$seniority = "$rank. ($percent)\n";
return $seniority;

Any ideas or help appreciated.

Regards,
Jason


Jürgen Exner

unread,
Dec 18, 2001, 3:52:31 PM12/18/01
to
"Jason Gray" <pe...@cableone.net> wrote in message
news:u1v5l1c...@corp.supernews.com...

> Ok, most of this I've gotten down but I want to be able to do this in an
> easier way for getting the rank and percent. Im going to 100% then want to
> stop.

Ok. English may not be my native language. But did anyone at all understood
what Jason is talking about?

> my file that it matches from looks like this, 0 being the value: key|0

????

jue

Steffen Müller

unread,
Dec 18, 2001, 5:22:34 PM12/18/01
to
"Jürgen Exner" <jurg...@hotmail.com> schrieb im Newsbeitrag
news:3c1f...@news.microsoft.com...

| Ok. English may not be my native language. But did anyone at all
understood
| what Jason is talking about?

Maybe not quite but... well...

Steffen Müller
^
:-)
--
$_=qq#tsee gmx.net#;s#e#s#g;s#[^\s\w]#c#;s#s#ust#g;s#t#J#e;s#nus#ker#
;chop;$c=' ^^^^ ';$c='12319';@c=split/(..)/,'8234130006710523';@d=split"3"
,$c;chop;' at ';s#(t)ustust#$1\0ano$1;.#;y#.; #ehr#;@_=$_;shift@c,substr
$_[0],$_,1,chr(ord(substr$_[0],$_)-shift@c)for$d[0]..$d[1];print"$_[0]\n";

Jason Gray

unread,
Dec 18, 2001, 6:05:04 PM12/18/01
to
Alright, well since its not understandable ill try to explain better.

I have a file that stores values of an binary ip address and rank(which adds
up everytime your ip visits the page), it is seperated by pipe characters.

Now what i want to do is show a rank and percent for every visit. My ranks
are stored in an array; eg: my(@ranks) = qw(newbie rookie elite);

Now i want to open up the file with the stored values to match up the ip and
read the rank value into $value.

open(FILE, $path) or die "Can't open $path. $!";
while(<FILE>) {
next if /^\s*$/;
chomp;
($key, $value) = split(/\|/, $_);
$match = $key == $ip;
last if $match;
}
close(FILE);

Now if the ip is matched, it reads from the $value which goes from 0-? and
for each value (0-?) i want it to display a rank from the array and a
percentage. There has to be an easier way of doing this:

Damian James

unread,
Dec 18, 2001, 6:23:49 PM12/18/01
to
On Tue, 18 Dec 2001 13:18:06 -0600, Jason Gray said:
>Ok, most of this I've gotten down but I want to be able to do this in an
>easier way for getting the rank and percent. Im going to 100% then want to
>stop.
>...

> if($match) {
> (($rank = @ranks[0]) && ($percent = "1%")) if $value == 1;
> (($rank = @ranks[1]) && ($percent = "2%")) if $value == 2;
> (($rank = @ranks[2]) && ($percent = "3%")) if $value == 3;
> (($rank = @ranks[3]) && ($percent = "4%")) if $value == 4;
> (($rank = @ranks[4]) && ($percent = "5%")) if $value == 5;
> (($rank = @ranks[5]) && ($percent = "6%")) if $value == 6;
> (($rank = @ranks[5]) && ($percent = "7%")) if $value == 7;
> (($rank = @ranks[6]) && ($percent = "8%")) if $value == 8;
> (($rank = @ranks[7]) && ($percent = "9%")) if $value == 9;
> (($rank = @ranks[8]) && ($percent = "10%")) if $value == 10;
> }
>
> $seniority = "$rank. ($percent)\n";
> return $seniority;
>

Ugh. Three problems:

First: @ranks[0] is an array slice consisting of one element. You
want to say is $ranks[0], which specifies a single value.

Second: are you sure you want to be using && here? If the
assignment on the left is and undefined value, an empty string
or to zero, then the assignment to the left will not occur.

Third: were you really going to make 100 if statements? :-).

What you want is probably something like [untested]:

die "Bad value: $value" if $value < 1 || $value > 100;
return "$rank[$value-1]. ($value%)\n" if $match;
or
unshift @rank, "No zeroth rank";
return "$rank[$value]. ($value%)\n" if $match;

But you might want to consider using a hash instead of an array
for @rank, since you are probably going to make the obi-wan error
at some point or other with this :-).

HTH,

Cheers,
Damian
--
@:=grep!(m!$/|#!..$|),split//,<DATA>;@;=0..$#:;while($:=@;){$;=rand
$:--,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift@;if$;[0]==@|;select$,,
$,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/} __END__
Just another Perl Hacker,### http://home.pacific.net.au/~djames.hub

Steffen Müller

unread,
Dec 18, 2001, 5:30:39 PM12/18/01
to
"Jason Gray" <pe...@cableone.net> schrieb im Newsbeitrag
news:u1v5l1c...@corp.supernews.com...

| Ok, most of this I've gotten down but I want to be able to do this in an
| easier way for getting the rank and percent. Im going to 100% then want to
| stop.
|
| my file that it matches from looks like this, 0 being the value: key|0
|
| my(@ranks) = qw(blah blah blah);
|
| open(FILE, $path) or die "Can't open $path. $!";
| while(<FILE>) {
| next if /^\s*$/;
| chomp;
| ($key, $value) = split(/\|/, $_);
| $match = $key == $ip;
| last if $match;

last if $key == $ip;
?
Anyway, is this a large file? Is it sorted? Probably neither, but if both
apply, you might want to do some indexing for a worst-case runtime below
O(N).

| }
| close(FILE);
|
| if($match) {
| (($rank = @ranks[0]) && ($percent = "1%")) if $value == 1;
| (($rank = @ranks[1]) && ($percent = "2%")) if $value == 2;
| (($rank = @ranks[2]) && ($percent = "3%")) if $value == 3;
| (($rank = @ranks[3]) && ($percent = "4%")) if $value == 4;
| (($rank = @ranks[4]) && ($percent = "5%")) if $value == 5;
| (($rank = @ranks[5]) && ($percent = "6%")) if $value == 6;
| (($rank = @ranks[5]) && ($percent = "7%")) if $value == 7;
| (($rank = @ranks[6]) && ($percent = "8%")) if $value == 8;
| (($rank = @ranks[7]) && ($percent = "9%")) if $value == 9;
| (($rank = @ranks[8]) && ($percent = "10%")) if $value == 10;

One *bug*. @ranks[?] is a slice. You are obviously not using use warnings;
(and probably not use strict;, too). That is very bad. ALWAYS use them. The
only frequently occurring reason to *locally* turn warnings off is deep
recursion. The only reason I can think of at least. I will not help you any
further unless you turn on strictures/warnings.

That's an ugly construct anyway, IMHO.

if ( $value > 0 && $value < 11 ) {
$rank = $ranks[$value];
# ^
# important.
$percent = $value . '%';
}

| }
|
| $seniority = "$rank. ($percent)\n";
| return $seniority;

maybe this, but not necessarily:

return "$rank. ($percent)\n";

use strict; #always
use warnings; #always

HTH
Steffen

0 new messages