-3

Ok so I have this line of code that I cant figure out and if someone could help me with it that would be great. I am making a geo-location perl script as part of my project.

Here is the line of code

$isps = $info->{'isp'};

if ($isps = "Time Warner Cable")
 {

  print "Isp found, go to $website for more information\n";       
}

if ($isps = "Google") {

    print "Isp found, go to $website for more information\n";       
} else {

    print "No ISP located! No way of Contact via this terminal!";
}

Ok so basically I am trying to make the if statement read the JSON code and make it print certain text if the specific name is listed. I am adding more ISPs to the file but just those two for example for now.

If anyone could help me with this line of code, because I seriously cant figure it out.

zdim
  • 64,580
  • 5
  • 52
  • 81
Roto
  • 25
  • 1
  • 1
  • 7
  • 2
    What is the problem _exactly_? – litelite Aug 24 '17 at 18:08
  • 1
    I thought OP was showing pseudo-code of some sort, so voted to close for that reason, but this should be closed either as a duplicate of [How do I compare two strings in Perl?](https://stackoverflow.com/q/1175390/100754) or as a typo. It would be great if someone can figure out a decent title. This has nothing to do with JSON, but I can't think of a decent, descriptive title. – Sinan Ünür Aug 24 '17 at 18:25
  • 1
    @SinanÜnür I had just voted to close as typographical error, not reproducable (etc) – zdim Aug 24 '17 at 18:28
  • 1
    Welcome to StackOverflow. Please read [How to Ask](https://stackoverflow.com/help/how-to-ask), and include details of what exactly is wrong with your approach. – Antimony Aug 24 '17 at 18:29
  • 1
    @SinanÜnür I removed that "JSON" from the title, at least for now – zdim Aug 24 '17 at 18:30
  • Please **always** add `use strict;` and `use warnings;` to your code, especially if you are a newbie. It would have shown `Found = in conditional, should be == at ./t.pl line 11.` If you'd then change that to `==` (which is still wrong, but a bit better), it says `Argument "bar" isn't numeric in numeric eq (==) at ./t.pl line 11.`. – PerlDuck Aug 25 '17 at 09:46

2 Answers2

5

The = is the assignment operator. You want the string comparison operator eq instead.

In this line you are assigning the string "Time Warner Cable" to the $isps variable. Then the if-conditional sees the string and interprets it as true. The same for the next conditional.

if ($isps = "Time Warner Cable")

Instead, you want:

my $isps = $info->{'isp'};

if ($isps eq "Time Warner Cable") {
    print "Isp found, go to $website for more information\n";       
}
elsif ($isps eq "Google") {
    print "Isp found, go to $website for more information\n";       
} else {
    print "No ISP located! No way of Contact via this terminal!\n";
}
amon
  • 57,091
  • 2
  • 89
  • 149
  • 2
    Probably worth noting - `use strict;` `use warnings;` tells you about this. `Found = in conditional, should be ==` – Sobrique Aug 25 '17 at 08:50
1

You should update your question to tell us what's going wrong with your current code.

One major problem is that = is the assignment operator. To compare strings for equality, use the eq operator:

if ($isps eq "Time Warner Cable") # ...

(The == operator performs numeric comparison.)

For more information, perldoc perlop, available online here.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • Wow, I feel dumb. Kinda knew to perl itself. I didnt realize it would be that easy. Thank you so much! Ill learn from my mistakes. – Roto Aug 24 '17 at 18:16