LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-06-2008, 01:49 PM   #1
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Rep: Reputation: 15
Slight Question in Perl


If I have data that I am going through that have names and they are located at $a[5] of the array IE:

BOB
JIM
TED
ADM-BOB
JILL
ADM-ADAM
GEORGE


How do I test to see if the name has the ADM in front of it I thought it was:

Code:
if ($a[5]=~ /^ADM-/i)  {
    statement
}

But this doesnt work. How I check that the Name has ADM- in front of it?
 
Old 11-06-2008, 06:20 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,344

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
Should work, except you're using 'i' switch which means ignore_case ie match ADM, adm, Adm etc.
If that doesn't fix it, please show code and or describe in more detail.
Have you checked carefully that there's no hidden chars at the start of the field eg <space> etc.
 
Old 11-07-2008, 09:09 AM   #3
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Exclamation

Basically I am looking for how to search the string for "adm-" the above does not seem to do it. Here is the sequence: When it runs, it never gets written to STAFF it goes to BAD. Some reason the "=~ /adm-/" seems as if it never runs. How do I get it to search the string for that!!

Code:
if (Sane($_))  {
      if ($a[0] =~ /\.public-labs/i)  {
        if (!&special($a[5]))  {
          print STAFF "$_\n";
        }
      }
      elsif ($a[0] =~ /\.circa\.units$/i)  {
        print STAFF "$_\n";
      } 
      elsif ($a[5] =~ /adm-/)  {
        print STAFF "$_\n";
      }
      elsif (&special($a[5]))  {
        print LOG "$_\n";
      }
      else  {
        print OUT "$_\n";
      }
}
else {
      print BAD "$_\n";
}
 
Old 11-07-2008, 10:10 AM   #4
teknik
Member
 
Registered: Jun 2006
Location: Winnipeg, Canada
Distribution: Slackware 12.1
Posts: 33

Rep: Reputation: 16
If it's going to BAD, then your
Code:
if(Sane($_))
is what's failing...

the expression /adm-/i should work fine.
 
Old 11-07-2008, 10:51 AM   #5
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
here is what is in the sane file

Code:
#!/usr/bin/perl

sub chkchrg  {
  local(@u) = @_;
  local($sane) = 0;
  %ratio = ("CL", 1000, "BW", 1000, "DJ", 1000);
  %size = ("CL", 1024, "BW", 1024, "DJ", 1024, "PL", 1024);
  %pgcount = ("CL", 1000, "BW", 1000, "DJ", 1000);

#  if ( ($u[2] =~ /dj/) && ($u[11] < 1) )  {
#    $sane = 1;
#  }
  ($p) = (split(/-/, $a[2]))[2];
  #print "\n$p\n";
  #print "\n$a[9]\n";
  #print "\n$a[10]\n";
  chop($p);
  # print "\n$p\n";
  if ( $a[9] < $size{$p})  {
      $sane = 1;
      $reason = " ********Size is too small  -  Size is $a[9]  ********";
  }
  elsif ( $a[10] > $pgcount{$p})  {
      $sane = 1;
      $reason = " ********Page count is to large  -  Page count is $a[10]  ********";
  }
  elsif ($a[10])  {
    if ( ($a[9] / $a[10]) < $ratio{$p})  {
      $sane = 1;
      $reason = " ********Bad Ratio********";
    }
  }
  
  $sane;
}

1;
 
Old 11-07-2008, 10:55 AM   #6
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
I got it to work, I moved it to before the if(Sane($_)), however it still should work. Any idea why it doesnt work the way I had it?
 
Old 11-07-2008, 01:29 PM   #7
teknik
Member
 
Registered: Jun 2006
Location: Winnipeg, Canada
Distribution: Slackware 12.1
Posts: 33

Rep: Reputation: 16
Well, in your code, there are only three ways for $sane to be set to 1: if $a[9] is too small, if $a[10] is [b]too large, or if $a[9]/$a[10] is a "bad ratio".

These appear to be "error cases", in that you want it to return false when these things happen. However, when they happen, you are setting $sane = 1. Should you not be setting $sane to 0? (a 0 is false, and a 1 is true). Otherwise, you are setting $sane = 1 (true) when there is an error occuring.

If this is the case, then you are going to also want to change your default value for $sane to a 1 as well.

Last edited by teknik; 11-07-2008 at 01:32 PM.
 
Old 11-07-2008, 02:39 PM   #8
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
up at the top I have


local($sane) = 0;

I have some debugging but why is that messing up if I did originally set sane to 0? And if it does help, I am actually modifying the person I took over for code.

Last edited by HyperTrey; 11-07-2008 at 02:42 PM.
 
Old 11-07-2008, 04:49 PM   #9
teknik
Member
 
Registered: Jun 2006
Location: Winnipeg, Canada
Distribution: Slackware 12.1
Posts: 33

Rep: Reputation: 16
Well 0 is a false. Therefore it will always return false, except when you are setting $sane to 1 (true), which you only do in those three if conditions. It seems to me that you would want this reversed. Right now, you've got it set up to return FALSE as long as there is NOT an error. If there IS an error, it returns TRUE. Shouldn't that be reversed? (right now script returns true on sane data, false if there was a problem). This is what I mean:

Code:
#!/usr/bin/perl

sub chkchrg  {
  local(@u) = @_;


# The default value is 1 (true), as in the data is SANE
  local($sane) = 1;
  %ratio = ("CL", 1000, "BW", 1000, "DJ", 1000);
  %size = ("CL", 1024, "BW", 1024, "DJ", 1024, "PL", 1024);
  %pgcount = ("CL", 1000, "BW", 1000, "DJ", 1000);

#  if ( ($u[2] =~ /dj/) && ($u[11] < 1) )  {
#    $sane = 1;
#  }
  ($p) = (split(/-/, $a[2]))[2];
  #print "\n$p\n";
  #print "\n$a[9]\n";
  #print "\n$a[10]\n";
  chop($p);
  # print "\n$p\n";

# when the script actually enters one of these if statements,
# then I assume that means an error has occured/data is not sane.
# (otherwise you wouldn't be printing error messages)
  if ( $a[9] < $size{$p})  {
      # therefore, set sane to 0 (false)
      $sane = 0;
      $reason = " ********Size is too small  -  Size is $a[9]  ********";
  }
  elsif ( $a[10] > $pgcount{$p})  {
      $sane = 0;
      $reason = " ********Page count is to large  -  Page count is $a[10]  ********";
  }
  elsif ($a[10])  {
    if ( ($a[9] / $a[10]) < $ratio{$p})  {
      $sane = 0;
      $reason = " ********Bad Ratio********";
    }
  }
  
  $sane;
}

1;
 
Old 11-10-2008, 02:08 AM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,344

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
A few general Perl comments:

1. Always use

use warnings;
use strict;

at the top of all Perl files.

2. in a sub (or anywhere really), you nearly always want

my $var

not

local($var)


3. Don't call a sub using &subname(), that's deprecated (as of V5), (drop the '&'). It now means ref-to-sub.

4. you call Sane(), but your checker sub is called chkchrg().

5. Its clearer if you

return($sane);

rather than just

$sane;

6. If you're removing the end-of-line char, its

chomp($p);

rather than

chop($p);
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Question for perl hackers. linuxlover1 Programming 1 06-27-2006 06:56 PM
reinstall perl question.. (urpme perl) rjcrews Mandriva 2 01-28-2006 06:19 PM
Hiding code in PERL, perl gui question randomx Programming 1 06-26-2004 03:22 PM
Slight error in this script & question dulaus Linux - Security 3 04-03-2004 10:48 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration