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
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
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";
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:
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
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