Professional Web Applications Themes

GetOptions problems - PERL Beginners

>  Actually you were probably 'sure', and it is probably failing for some 'reason'.   'someone' probably will help, but I don't think there are any 'one's here.   or even a 'useful' format.   Nope not 'payed' but possibly 'paid', and very rarely would anyone pay to 'dev' a script as opposed to 'develop' one. and it probably 'makes' your job 'easier'.   Hopefully you are 'learn'ing Perl.   Start with a spell checker, use punctuation, work on the grammar. Have a look at: http://www.catb.org/~esr/faqs/smart-questions.html#writewell Specifically, "Write in clear, grammatical, correctly-spelled language We've found by experience that people who ...

  1. #1

    Default Re: GetOptions problems



    Actually you were probably 'sure', and it is probably failing for some
    'reason'.
     

    'someone' probably will help, but I don't think there are any 'one's here.
     

    or even a 'useful' format.
     

    Nope not 'payed' but possibly 'paid', and very rarely would anyone pay
    to 'dev' a script as opposed to 'develop' one. and it probably 'makes'
    your job 'easier'.
     

    Hopefully you are 'learn'ing Perl.
     

    Start with a spell checker, use punctuation, work on the grammar. Have
    a look at:

    http://www.catb.org/~esr/faqs/smart-questions.html#writewell

    Specifically,

    "Write in clear, grammatical, correctly-spelled language

    We've found by experience that people who are careless and sloppy
    writers are usually also careless and sloppy at thinking and coding
    (often enough to bet on, anyway). Answering questions for careless and
    sloppy thinkers is not rewarding; we'd rather spend our time elsewhere.

    So expressing your question clearly and well is important. If you can't
    be bothered to do that, we can't be bothered to pay attention. "

    The whole page has valuable insights.
     
    ($inFile,$USAGE,$showKey,$site,$found,$foundKeys, dataFile,foundSegments,$value);

    Declaring all of your variables up front defeats the purpose of 'strict'
    and makes it far less useful. You should declare your variables at first
    usage and in the proper scopes.

     

    The above is ok, but you might consider taking the advice of the
    Getopt::Long docs and using Pod::Usage to generate error messages and
    help text via pod.
     

    You should drop the C<&> it is not needed in this context. You should
    also go back to the docs for Getopt::Long, it does not have the default
    $opt_ variables for the options, it instead uses references or a hash. I
    assume this is left over from Getopt::Std, and in that case you would
    have to declare your $opt_ variables with C<our> instead of C<my>.
     

    Docs can be found at
    http://search.cpan.org/~jv/Getopt-Long-2.34/lib/Getopt/Long.pm

    And I have an example template that uses Getopt::Long and incorporates
    Pod::Usage available here:

    http://danconia.org/cgi-bin/request?handler=Content;content=StaticPage;label=g etopt_long_template

    <snip>

    http://danconia.org
    Wiggins Guest

  2. #2

    Default GetOptions problems

    ok ill send again from the correct address...
     
    'reason'.
    ....
     
    a look at:

    yes i know i can not spell, im a dislexic moron
    thank you i will read the page from the link.


     
    ($inFile,$USAGE,$showKey,$site,$found,$foundKeys, dataFile,foundSegments,$value);

    Declaring all of your variables up front defeats the purpose of 'strict'
    and makes it far less useful. You should declare your variables at first
    usage and in the proper scopes.

    are i allways beleaved you should declare them like this...
    how should i?? some thing like:

    my $USAGE = ...

    and will this work if the first time the variables is used is in a loop?
     
     
    help text via pod.

    ill look in to this. i have been using "O'reilly's Programming Perl
    /Learning Perl" which say nothing of using Pod
     
     
    also go back to the docs for Getopt::Long, it does not have the default
    $opt_ variables for the options, it instead uses references or a hash. I
    assume this is left over from Getopt::Std, and in that case you would
    have to declare your $opt_ variables with C<our> instead of C<my>.


     

    Docs can be found at
    http://search.cpan.org/~jv/Getopt-Long-2.34/lib/Getopt/Long.pm

    And I have an example template that uses Getopt::Long and incorporates
    Pod::Usage available here:

    http://danconia.org/cgi-bin/request?handler=Content;content=StaticPage;label=g etopt_long_template

    <snip>

    http://danconia.org



    ok i have had a look at the cpan doc and made some adjustments...
    it will take me a while to digest the pod bit though


    also i am having a problem with the
    #Data collections / inputfile part

    " foreach $value (dataFile) { # loop for each line/site in dataFile
    chomp $value ;
    foundSegments=findVars($searchKey,$value);"


    it is only storing the last value should i be doing something like

    "foundSegments[$i]=findVars($searchKey,$value);
    $i++"

    thank you for your help...

    RichT

    New code============================================== =========
    #!/usr/local/bin/perl
    #

    use strict;
    use warnings;
    use Getopt::Long;

    Getopt::Long::config qw(no_ignore_case);

    my
    ($inFile,$USAGE,$showKey,$site,$found,$foundKeys, dataFile,foundSegments,$value);

    $USAGE = <<USAGETEXT;
    usage: $0 ipaddress
    the following options are also availble
    [-inFile filename ] input filename
    [-findField fieldName ] this is the search key (default is
    agentAddress)
    [-showFields field names ] feilds to output (default is
    segment,agentAddress,communi
    ty)
    [-listAllFields ] list avalible fields
    [-help] this help text
    USAGETEXT
    my $needHelp = '' ;
    my $outputAllFields = '' ;
    my $inputFile = '' ;
    my $searchKey = "agentAddress";
    my $outputFields = "segment,agentAddress,community";

    GetOptions( "inFile=s" => \$inputFile,
    "findField=s" => \$searchKey,
    "showFields=s" => \$outputFields,
    "listAllFields" => \$outputAllFields,
    "help|h|H|?|HELP" => \$needHelp
    );

    if ($needHelp) {print $USAGE; exit; } # check to see if this is a request
    for help, if so prin
    t USAGE

    ################################################## ##########
    # Data collections
    # if we using an input file?
    # else if we have found some values on the cl
    # else quit
    ################################################## ##########

    if ($inputFile){ # find results if we have in -inFile

    open DFILE, "$inputFile" # open $inputFile and read in or die
    or die " could not open $inputFile";
    dataFile = <DFILE>;
    close DFILE;

    foreach $value (dataFile) { # loop for each line/site in dataFile
    chomp $value ;
    foundSegments=findVars($searchKey,$value);
    }

    } elsif ($ARGV[0]) { # read in value from comandline

    foreach $value ($ARGV[0]) { # loop for each line/site in dataFile
    foundSegments=findVars($searchKey,$value);
    }

    } else {print " could not find any input \n $USAGE "; exit; }

    ################################################## ##########
    # Data output
    # if request for keys print all keys
    # else print results
    ################################################## ##########

    if ($outputAllFields) {
    for $found ( foundSegments ) {
    print "\n" ;
    for $foundKeys (keys %$found) {
    print "$foundKeys,";
    }
    }

    } else {

    for $found ( foundSegments ) {
    foreach $showKey (split /,/, $outputFields) {
    print "$found->{$showKey},";
    }
    print "\n";
    }

    }

    sub findVars {
    ################################################## ##########
    # Function Check Discover Results
    # Parameters :
    #
    # Returns :
    #
    #
    ################################################## ##########


    my($findKey, $findValue, $segmentFieldKey, $segmentFieldValue,
    %segmentFields, $nullVar, foun
    dSegments);

    # read in Search Key and Value from parent NOTE make a check for this
    $findKey=$_[0] || die "Missing Args $findKey $!" ;
    $findValue=$_[1] || die "Missing Args $findValue $!" ;
    chomp $findValue;
    chomp $findKey;
    #my $NH_HOME= $ENV[NH_HOME]; # point to the poller CFG file
    my $NH_HOME= "."; # point to the poller CFG file NOTE this is temp for
    testing use above line
    in live


    local $/ = "}\n"; # set delimiter


    open(POLLER, "$NH_HOME/poller.cfg") || die "can not open : $!";

    #s/universalPollList \{//g;

    while(<POLLER>) {
    next unless /^\s+segment/;
    s/\n\s+\}\n//g;
    s/["{]//g;
    foreach (split(/\n/)) {
    ($nullVar,$segmentFieldKey,$segmentFieldValue) =
    split(/\s+/,$_,3);
    $segmentFields{ $segmentFieldKey } = $segmentFieldValue ;
    }
    if ( $segmentFields{$findKey} eq $findValue ) {

    push foundSegments, {%segmentFields } ;
    }
    undef %segmentFields;
    my %segmentFields;
    }
    close POLLER;

    return (foundSegments); # return the IP and comunity string to main
    ruteen
    };



    Cats Guest

  3. #3

    Default Re: GetOptions problems

    > ok ill send again from the correct address... 
    > 'reason'.
    > ...

    > a look at:
    >
    > yes i know i can not spell, im a dislexic moron
    > thank you i will read the page from the link.
    >[/ref]

    Dyslexia does not make one a moron, though it must make programming
    significantly more challenging. I thought something might be up, which
    is why my comments were made, but not intentionally hurtful. Sometimes
    people really are that lazy....
     
    >[/ref]
    ($inFile,$USAGE,$showKey,$site,$found,$foundKeys, dataFile,foundSegments,$value); 

    Yes. There is no hard rule, the variable should be declared at the
    tightest scope possible. So if the variable is only needed in a loop,
    then declare it in the loop. If it needs a less specific scope, then
    scope it where you need to. For more on scoping check out:

    http://perl.plover.com/FAQs/Namespaces.html

     

    > help text via pod.
    >
    > ill look in to this. i have been using "O'reilly's Programming Perl
    > /Learning Perl" which say nothing of using Pod
    >[/ref]

    Yes the first is a reference about language features rather than a good
    source of best practices, the second is excellent for teaching, but as
    such tends to keep snippets short in the interest of teaching the
    specific subject at hand. Both are excellent for their intended purpose,
    which is not always developing full programs.
     

    > also go back to the docs for Getopt::Long, it does not have the default
    > $opt_ variables for the options, it instead uses references or a hash. I
    > assume this is left over from Getopt::Std, and in that case you would
    > have to declare your $opt_ variables with C<our> instead of C<my>.
    >
    >

    >
    > Docs can be found at
    > http://search.cpan.org/~jv/Getopt-Long-2.34/lib/Getopt/Long.pm
    >
    > And I have an example template that uses Getopt::Long and incorporates
    > Pod::Usage available here:
    >
    >[/ref]
    http://danconia.org/cgi-bin/request?handler=Content;content=StaticPage;label=g etopt_long_template 

    They often do, being able to read the docs and pull from the API is
    probably the most important skill in Perl (programming in general).
     
    dataFile 

    Generally when looping over a file you should use a C<while> construct
    instead to reduce memory overhead.
     

    This would be a very C way of doing it, which is ok, but in Perl we are
    provided with the C<push> function, which is signifcantly easier,

    perldoc -f push

    push foundSegments, findVars($searchKey, $value);
     

    Keep at it...

    http://danconia.org
     
    <snip>
    Wiggins Guest

  4. #4

    Default Re: GetOptions problems


    On Sep 30, 2004, at 10:35 AM, cats wrote:
     

    If you're like other dyslexics I've known, you're probably (a) not a
    moron and (b) exceptionally brilliant in other areas.

    Thanks for the reminder.

    Chap Guest

Similar Threads

  1. Problems with CFC
    By raulriera in forum Macromedia ColdFusion
    Replies: 0
    Last Post: April 1st, 03:47 AM
  2. Replies: 0
    Last Post: September 22nd, 02:18 PM
  3. DBI-PG problems
    By culley harrelson in forum Ruby
    Replies: 6
    Last Post: August 1st, 04:08 PM
  4. VSS problems
    By Harry Simpson in forum ASP.NET General
    Replies: 0
    Last Post: July 15th, 03:43 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139