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:
a weak mind want to know :)

 



Aquilo
journeyman

Jan 17, 2001, 11:10 PM

Post #1 of 10 (1190 views)
a weak mind want to know :) Can't Post

OK what am I doing wrong I've tryed many ways of spliting and printingand going crazy...
How do I get this to print print "Location: ".\?Community\=Shake\&Bake\=.$dig[$i].".gif\n\n"; for each digit?
---------------------------------------------------------------------------
$v1 = 12;
$v2 = 1;
$v3 = 235;
$v4 = 33;
$v5 = 1200002;
$v6 = 5;
$v7 = 10;

$digit = $v1 + $v2 + $v3 + $v4 + $v5;
$digitx = $v7 * $v6;
$digits = $digit * $digitx;

# Still not working
#$digitz = $digits;
#$digitz = length($digitz);
#$digitz = split(//, $digitz);
#for ($i = 0; $i <= $digitz; ++$i) {
#$dig = $digitz[$i];
#foreach $dig ($digitz) {
#print "Location: ".\?Community\=Shake\&Bake\=.$dig[$i].".gif\n\n";
#}# HELP!
print "$digits\n\n"; #Works



Aquilo
journeyman

Jan 18, 2001, 12:11 AM

Post #2 of 10 (1184 views)
Re: a weak mind want to know :) [In reply to] Can't Post

I have got it working for the most part only it's displaying an extra "spot" that should not be in the array, any one know how to kill the extra space?
<img border="0" src="http://xtram.com/communities/images/6.gif" alt="6">
<img border="0" src="http://xtram.com/communities/images/0.gif" alt="0">
<img border="0" src="http://xtram.com/communities/images/0.gif" alt="0">
<img border="0" src="http://xtram.com/communities/images/1.gif" alt="1">
<img border="0" src="http://xtram.com/communities/images/4.gif" alt="4">
<img border="0" src="http://xtram.com/communities/images/1.gif" alt="1">
<img border="0" src="http://xtram.com/communities/images/5.gif" alt="5">
<img border="0" src="http://xtram.com/communities/images/0.gif" alt="0">
<img border="0" src="http://xtram.com/communities/images/.gif" alt="">
< br >
60014150

###########################################
$v1 = 12;
$v2 = 1;
$v3 = 235;
$v4 = 33;
$v5 = 1200002;
$v6 = 5;
$v7 = 10;

$digit = $v1+$v2+$v3+$v4+$v5;
$digitw = $v7*$v6;
$digits = $digit * $digitw;

$digitz = $digits;
@digitx = split(//, $digitz);
for ($i = 0; $i <= @digitx; ++$i) {
$num = @digitx[$i];
$dig = @digitx[$i];
print "<img border=\"0\" src=\"";
print "http://xtram.com/communities/images/$dig.gif\" alt=\"$num\">\n";
}
print "< br >";
print "$digits\n\n";
###########################################
http://xtram.com/communities/community.pl?Community=Ev90&Action=Member&Action2=Test2

Thanks for any help!!!


sleuth
Enthusiast

Jan 18, 2001, 1:39 AM

Post #3 of 10 (1182 views)
Re: a weak mind want to know :) [In reply to] Can't Post

 
Yikes Aquilo stop, hehe.

Ok, there are two cleaner ways I like to do stuff like that, with a hash, or array. I like using a hash, so lets do that.

$v{'1'} = 12;
$v{'2'} = 1;
$v{'3'} = 235;
$v{'4'} = 33;
$v{'5'} = 1200002;
$v{'6'} = 5;
$v{'7'} = 10;

foreach $dig (values %v){
print "http://xtram.com/communities/images/$dig.gif\" alt=\"$dig\">\n";
}

See if you can swing something like that to suit your needs mate,

Sleuth



Aquilo
journeyman

Jan 18, 2001, 1:05 PM

Post #4 of 10 (1171 views)
Re: a weak mind want to know :) [In reply to] Can't Post

Thanks sleuth! I tryed every thing I could to find a way to split the hash becouse they have to be seperate numbers but I could not find documentation - but I did learn new things searching :)

I still can't find the problem that makes an empty image at the end of the number!

<img border="0" src="?Community=Shake&Bake=2.gif" alt="2">
<img border="0" src="?Community=Shake&Bake=5.gif" alt="5">
<img border="0" src="?Community=Shake&Bake=1.gif" alt="1">
<img border="0" src="?Community=Shake&Bake=.gif" alt="">

I did take out every thing except what will go in the final version as far as name=value pairs I only need 3 for now and it works pritty quick!


Code
	$v{'1'}	=15,$v{'2'}=1,$v{'3'}=235; 
$count1 = $v{'1'}+$v{'2'}+$v{'3'};
@count = split(//, $count1);
for ($i = 0; $i <= @count; ++$i) {
$dig = @count[$i];
print "<img border=\"0\" src=\"\?Community\=
Shake\&Bake\=$dig.gif\" alt=\"$dig\">\n";
}

but how do you split (values %x) into each numder once they have been added togeather using
foreach $dig (values %x){



sleuth
Enthusiast

Jan 18, 2001, 1:33 PM

Post #5 of 10 (1167 views)
Re: a weak mind want to know :) [In reply to] Can't Post

 Ok, you lost me, I gave your the way to do it using a hash but you still use,

$v{'1'} =15,$v{'2'}=1,$v{'3'}=235;
$count1 = $v{'1'}+$v{'2'}+$v{'3'};
@count = split(//, $count1);

What are you trying to do exactly, I'm confused there,

The reason you get

<img border="0" src="?Community=Shake&Bake=2.gif" alt="2">
<img border="0" src="?Community=Shake&Bake=5.gif" alt="5">
<img border="0" src="?Community=Shake&Bake=1.gif" alt="1">
<img border="0" src="?Community=Shake&Bake=.gif" alt="">


Is because your not looping through the array correctly, you are using ++$i and that starts counting at 1. When the array starts at 0. Then it counts 1 extra time at the end. It's just offset by 1, so it misses the first and last value in the array.

You could even do this,

foreach $dig (@count){
print qq~<img border="0" src="?Community=Shake&Bake=$dig.gif" alt="$dig">~;
}

Which is a lot easier.


Instead of this three step process,

$v{'1'}=15,$v{'2'}=1,$v{'3'}=235;
$count1= $v{'1'}+$v{'2'}+$v{'3'};
@count= split(//, $count1);

You could actually make this a one step process,

push(@count,"15 2 1 235");

Then loop through that.

What do you mean split (values %x) into each numer, I don't understand the question. Do you want to know how to get 1 and get 15 out of $v{'1'}?

Sleuth



japhy
Enthusiast

Jan 18, 2001, 3:21 PM

Post #6 of 10 (1163 views)
Re: a weak mind want to know :) [In reply to] Can't Post

No, sleuth...


Code
($x,$y,$z) = (15, 1, 235); 
$number = $x + $y + $z; # this is 251, NOT 151235

Here's how I'd go about doing it:


Code
my $x; 
$x += $_ for keys %v;
for (split //, $x) {
print image_for($_);
}

sub image_for {
my $n = shift;
return qq(<img src="digit-$n.gif" alt=$n>)
}

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


Aquilo
journeyman

Jan 18, 2001, 3:28 PM

Post #7 of 10 (1163 views)
Re: a weak mind want to know :) [In reply to] Can't Post

In the end the values will be pulled remotely from another site and they wont be static anymore so I
have to leave it open to get the value in there. I guess
push(@count,"15 1 235"); could be push(@count,"$v1 $v2 $v3");

But I cant have

<img border="0" src="?Community=Shake&Bake=15.gif" alt="15">
<img border="0" src="?Community=Shake&Bake=1.gif" alt="1">
<img border="0" src="?Community=Shake&Bake=235.gif" alt="235">

and thats what I get with

foreach $dig (@count){

I need to add the three values then split that into

<img border="0" src="?Community=Shake&Bake=2.gif" alt="2">
<img border="0" src="?Community=Shake&Bake=5.gif" alt="5">
<img border="0" src="?Community=Shake&Bake=1.gif" alt="1">

becouse I only have 1-9.gif or 9 images so I can't have the end value of 251.gif



Aquilo
journeyman

Jan 18, 2001, 4:15 PM

Post #8 of 10 (1162 views)
Re: a weak mind want to know :) [In reply to] Can't Post

japhy?? question? if I use

Code
	%v = (15,1,235); 

my $x;
$x += $_ for keys %v;
for (split //, $x) {
print image_for($_);

}

sub image_for {
my $n = shift;
return qq(<img border=\"0\" src=\"\?
Community\=Shake\&Bake\=$n.gif\" alt=\"$n\">)
}

It's not counting right I get 250 not 251
and if I use

Code
	%v = ($v{'1'},$v{'2'},$v{'3'});

it don't count the values it counts the number of values returning a 3



sleuth
Enthusiast

Jan 18, 2001, 6:47 PM

Post #9 of 10 (1155 views)
Re: a weak mind want to know :) [In reply to] Can't Post

 
Oops, I'm really sorry,

push(@count,"what I had");

won't work at all, I'm very sorry. I need to sleep more often.

I should have just said,

@count = (15,1,3243);

And kept going like that, then it'll work.

I'm very sorry, I think I'm going to bow out now,



japhy
Enthusiast

Jan 18, 2001, 7:26 PM

Post #10 of 10 (1153 views)
Re: a weak mind want to know :) [In reply to] Can't Post

You lose points for not using the -w switch on your Perl programs. You're mis-using a hash. You have an odd number of elements. Perhaps you meant to be using an array:


Code
my @values = (15, 1, 135); 
my $x = 0;
$x += $_ for @values;
# the split thing, etc.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

 
 


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

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