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:
the error of my ways?

 



ultimategamer2k
User

Sep 3, 2001, 2:29 PM

Post #1 of 6 (1698 views)
the error of my ways? Can't Post

there seems to be a problem with this

foreach $temp2 (@listoutput){
$temp2 = $listoutput[$e];
reverse $temp2;
while ($f<=9){
chop $temp2;
$f=$f+1;
}
reverse $temp2;
$listoutput[$e] = $temp2;
$e=$e+1;
}
print "<hr>";
foreach $temp (@listoutput){
print "$temp
";
}
print "<HR>";

but i dont know where!
can someone help me? Its probably something very trivial



mhx
Enthusiast / Moderator

Sep 3, 2001, 9:34 PM

Post #2 of 6 (1695 views)
Re: the error of my ways? [In reply to] Can't Post

What are you expecting this to do?
Either post a detailed description or (and) post a little more of your code.
I guess there's some foreach above the code you posted and I can even think of what's going wrong, but I would appreciate seeing a little more code and a short description of what this is supposed to do before.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



ultimategamer2k
User

Sep 4, 2001, 3:53 AM

Post #3 of 6 (1691 views)
Re: the error of my ways? [In reply to] Can't Post

sorry - i couldnt think of any other way to get rid of 9 characters in front of all the arrays values, so i thought id reverse it, chop it, then reverse it again.



mhx
Enthusiast / Moderator

Sep 4, 2001, 4:57 AM

Post #4 of 6 (1689 views)
Re: the error of my ways? [In reply to] Can't Post

Ok, that's at least a starting point.
Say you have an array of strings:

Code
my @strings = ( 
'Hello World!',
'This is a test...',
'We kill 9 characters!',
);

and want to remove the first 9 characters of each string in the array, then use the following handy one-liner:

Code
substr $_, 0, 9, '' for @strings;

Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



ultimategamer2k
User

Sep 4, 2001, 5:16 PM

Post #5 of 6 (1683 views)
Re: the error of my ways? [In reply to] Can't Post

wow, thanks!
ill use yours for sure. But just out of curiosities sake, what is wrong with mine?



mhx
Enthusiast / Moderator

Sep 4, 2001, 9:21 PM

Post #6 of 6 (1679 views)
Re: the error of my ways? [In reply to] Can't Post


In Reply To
But just out of curiosities sake, what is wrong with mine?

It's far too complicated... Wink

I'd say you don't reset $f to zero before entering the while loop. So it will chop off the chars only from the first string.
Besides, even without knowing substr, this could have been written a lot easier as

Code
foreach my $temp ( @listoutput ) { 
$temp = reverse $temp;
chop $temp for 1..9;
$temp = reverse $temp;
}

Always keep in mind that the foreach loop automatically puts each element of @listoutput into $temp, and directly modifies the element of @listoutput if $temp is modified inside the loop.
However, I'd use substr, because it's the most efficient, most perlish, and shortest way.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"


 
 


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

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