Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Perl Issues


TheSilentDrifter's Avatar
Member
0 0

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.


ghost's Avatar
0 0

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] &lt;= 25.00);

Probably won't work though, like I said before, I suck.


TheSilentDrifter's Avatar
Member
0 0

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] &lt;= 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] &gt;= 25.00) {
 print &quot;@split[1] @split[0] - &#92;$@split[3]&#92;n&quot; ;
}

ghost's Avatar
0 0

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?


ghost's Avatar
0 0

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.


ghost's Avatar
0 0

I only know a little basic Perl, but I think when you index arrays you use $ instead of @.


TheSilentDrifter's Avatar
Member
0 0

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, &quot;&lt;telData.txt&quot; || die &quot;Could not open the file!&#92;n&quot;;
my(@split);
while(&lt;TEL&gt;) {
  @split = split /&#92;|/, &lt;TEL&gt;;
  print &quot;@split[1] @split[0] - &#92;$@split[3]&#92;n&quot; if(@split[3] &gt;= 25.00);
}

TheSilentDrifter's Avatar
Member
0 0

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.


TheSilentDrifter's Avatar
Member
0 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.


ghost's Avatar
0 0

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 &quot;end&quot;;```

Run it and see.

Edit: Oh, mixed up newlines with spaces here :P.

TheSilentDrifter's Avatar
Member
0 0

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 &quot;end&quot;;```

Run it and see.[/quote]

I think i know what you&#39;re saying. I need to chomp it? (Remove the &#92;n)

ghost's Avatar
0 0

Yeah it was just a suggestion, I dont have perl installed atm.

It was a common problem people overlook in other languages.


TheSilentDrifter's Avatar
Member
0 0

yeah, i tried chomping it, but it was a no go. Still only outputs 3 of the people on the list… Any other ideas?


ghost's Avatar
0 0

TheSilentDrifter wrote: yeah, i tried chomping it, but it was a no go. Still only outputs 3 of the people on the list… Any other ideas?

Your program skips line 1, 3, 5 etc. of telData.txt.


TheSilentDrifter's Avatar
Member
0 0

Mephisto wrote: [quote]TheSilentDrifter wrote: yeah, i tried chomping it, but it was a no go. Still only outputs 3 of the people on the list… Any other ideas?

Your program skips line 1, 3, 5 etc. of telData.txt. [/quote]

How do i get it to stop skipping the odd lines?


ghost's Avatar
0 0

TheSilentDrifter wrote: How do i get it to stop skipping the odd lines?

I have absolutely 0.00 clues :/.


TheSilentDrifter's Avatar
Member
0 0

Mephisto wrote: [quote]TheSilentDrifter wrote: How do i get it to stop skipping the odd lines?

I have absolutely 0.00 clues :/.[/quote]

damn lol… thanks for the help though.


ghost's Avatar
0 0

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."


TheSilentDrifter's Avatar
Member
0 0

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.


ghost's Avatar
0 0
use strict;
open TEL, &quot;&lt;telData.txt&quot; || die &quot;Could not open the file!&#92;n&quot;;
my(@split);
$a = &lt;TEL&gt;;
while($a) {
@split = split /&#92;|/, &lt;TEL&gt;;
print &quot;@split[1] @split[0] - &#92;$@split[3]&#92;n&quot; if(@split[3] &gt;= 25.00);
}```

Works.

TheSilentDrifter's Avatar
Member
0 0

Mephisto wrote:

use strict;
open TEL, &quot;&lt;telData.txt&quot; || die &quot;Could not open the file!&#92;n&quot;;
my(@split);
$a = &lt;TEL&gt;;
while($a) {
@split = split /&#92;|/, &lt;TEL&gt;;
print &quot;@split[1] @split[0] - &#92;$@split[3]&#92;n&quot; if(@split[3] &gt;= 25.00);
}```

Works.

yep, thanks for the help =)