Perl Issues
I'm trying to run this thread as a searching device that searches a specific files for stuff… I have this:
#!/usr/bin/perl use strict; open TEL, "<telData.txt" || die "Could not open the file!\n"; my(@split); while(<TEL>) { @split = split /\|/, <TEL>; print "@split[1] @split[0] - \$@split[3]\n" if(@split[3] >= 25.00); }
but the if(@split[3] >= 25.00); isn't searching as a decimal. I REALLY need help with this, because I'm on a deadline, and it has to be done by 12:00 M.S.T.
TheSilentDrifter wrote: I'm trying to run this thread as a searching device that searches a specific files for stuff… I have this:
#!/usr/bin/perl use strict; open TEL, "<telData.txt" || die "Could not open the file!\n"; my(@split); while(<TEL>) { @split = split /\|/, <TEL>; print "@split[1] @split[0] - \$@split[3]\n" if(@split[3] >= 25.00); }
but the if(@split[3] >= 25.00); isn't searching as a decimal. I REALLY need help with this, because I'm on a deadline, and it has to be done by 12:00 M.S.T.
It looks like you are mixing strings with numbers, I think. I dunno, 'cause I suck at coding. Perhaps you can change the if statement to this:
markup!if(@split[3] <= 25.00);
Probably won't work though, like I said before, I suck.
Mephisto wrote: [quote]TheSilentDrifter wrote: I'm trying to run this thread as a searching device that searches a specific files for stuff… I have this:
#!/usr/bin/perl use strict; open TEL, "<telData.txt" || die "Could not open the file!\n"; my(@split); while(<TEL>) { @split = split /\|/, <TEL>; print "@split[1] @split[0] - \$@split[3]\n" if(@split[3] >= 25.00); }
but the if(@split[3] >= 25.00); isn't searching as a decimal. I REALLY need help with this, because I'm on a deadline, and it has to be done by 12:00 M.S.T.
It looks like you are mixing strings with numbers, I think. I dunno, 'cause I suck at coding. Perhaps you can change the if statement to this:
markup!if(@split[3] <= 25.00);
Probably won't work though, like I said before, I suck.[/quote]
Nah, i know i have the if statement right. It's just in the form of an expression modifier. it means the same thing as:
if (@split[3] >= 25.00) {
print "@split[1] @split[0] - \$@split[3]\n" ;
}
are there only 3 elements in the @split array?
Maybe the last one is picking up a new line character or something from the file.
Can you strip trailing whitespace or something?
telData.txt:
StRegis|Les|200|15.26|Courtland|NM|216|723-6122
Zissily|Jeb|196|35.21|Elfrieda|AZ|711|574-5587
Brown|Rubin|231|10.14|Lochiel|CA|703|511-3745
Jarvis|Sue|340|15.15|Gleason|AZ|520|356-1210
Atara|Frieda|245|25.29|Courtland|NM|216|794-3876
Smith|Samuel|343|12.36|Sunnyside|AZ|314|457-7989
Jones|Omar|102|42.22|Harshawk|AZ|602|294-7034
Mason|Alice|221|21.28|Elfrieda|AZ|711|654-4458
Winnet|Alex|346|32.19|Fairbank|AK|908|632-3151
Smith|David|150|43.43|Sierra Vista|AZ|520|487-7989
Mason|Jimbo|334|14.37|Gleason|AZ|520|378-4458
Stark|Bonnie|340|18.16|Lochiel|CA|703|535-52032
q6.pl
#!/usr/bin/perl
use strict;
open TEL, "<telData.txt" || die "Could not open the file!\n";
my(@split);
while(<TEL>) {
@split = split /\|/, <TEL>;
print "@split[1] @split[0] - \$@split[3]\n" if(@split[3] >= 25.00);
}
sharpskater80 wrote: I only know a little basic Perl, but I think when you index arrays you use $ instead of @.
Only if you're pulling from the array itself. I'm pulling from the file and it's split. So basically on the first file it's taking out all the |'s and it's taking each section as a number starting with 0.
Mephisto wrote: Wait, do you want your if statement to limit itself to just two decimals?
Like so:
25.001 FAILS 25.01 DOESN'T FAIL
If that's what you want you need a new variable with the first two decimals only, and check if they're >= 0.
i only need to check the decimal against the other decimals. So it should already have it. But i'll try to set it as a formated variable.
Johnson wrote: are there only 3 elements in the @split array?
Maybe the last one is picking up a new line character or something from the file.
Can you strip trailing whitespace or something?
Perl eats those spaces:
$num = 1.23 ;
print $num;
print "end";```
Run it and see.
Edit: Oh, mixed up newlines with spaces here :P.
Mephisto wrote: [quote]Johnson wrote: are there only 3 elements in the @split array?
Maybe the last one is picking up a new line character or something from the file.
Can you strip trailing whitespace or something?
Perl eats those spaces:
$num = 1.23 ;
print $num;
print "end";```
Run it and see.[/quote]
I think i know what you're saying. I need to chomp it? (Remove the \n)
TheSilentDrifter wrote: damn lol… thanks for the help though.
WAIT. Found something: http://prlmnks.org/html/498172.html
Relevant bit (on the 'why-the-skip' question): "Because you read a line in the while, and then discard that line, and read the next line in your unless statement."
Mephisto wrote: [quote]TheSilentDrifter wrote: damn lol… thanks for the help though.
WAIT. Found something: http://prlmnks.org/html/498172.html
Relevant bit (on the 'why-the-skip' question): "Because you read a line in the while, and then discard that line, and read the next line in your unless statement."[/quote]
coolness i'll see if it can be used or not.
Mephisto wrote:
use strict;
open TEL, "<telData.txt" || die "Could not open the file!\n";
my(@split);
$a = <TEL>;
while($a) {
@split = split /\|/, <TEL>;
print "@split[1] @split[0] - \$@split[3]\n" if(@split[3] >= 25.00);
}```
Works.
yep, thanks for the help =)