Professional Web Applications Themes

Check on array return result - PERL Beginners

Hi, The while loop while(<check_vip_ports>) { $token=pop(check_vip_ports); .... } It is exiting before traversing the entire array. On running it on debug I found that a function in the perl library is returning a values which causes the exit as follows:- File::Glob::csh_glob(/usr/lib/perl5/5.8.2/sparc-linux/File/Glob.pm:137): 137: my $pat = shift; DB<11> x $_ 0 8256 DB<12> r scalar context return from File::Glob::csh_glob: undef Anyone familiar with this situation. Philip...

  1. #1

    Default Check on array return result

    Hi,

    The while loop

    while(<check_vip_ports>)
    {

    $token=pop(check_vip_ports);
    ....


    }

    It is exiting before traversing the entire array. On running it on debug
    I found that a function in the perl library is returning a values which
    causes the exit as follows:-

    File::Glob::csh_glob(/usr/lib/perl5/5.8.2/sparc-linux/File/Glob.pm:137):
    137: my $pat = shift;
    DB<11> x $_
    0 8256
    DB<12> r
    scalar context return from File::Glob::csh_glob: undef

    Anyone familiar with this situation.

    Philip
    Philip Guest

  2. #2

    Default Re: Check on array return result

    > Hi, 

    In the above you are passing the values in the list check_vip_ports to
    the file globbing operator <>, which is not likely what you want to be
    doing (let us know if it is for sure what you want to do).
     

    'pop' also removes the elements from the array which is usually not
    necessary, in the case that it is you should probably loop over the
    array using indexes instead of the standard 'foreach' idiom, then your
    while loop will make a bit more sense.
     

    See if the common,

    foreach my $token (check_vip_ports) {
    # work on $token here
    }

    Will work. If not show us more of what you are doing, what
    check_vip_ports contains and what you are doing with $token...

    Turn on 'use strict'/'use warnings' if you haven't...

    http://danconia.org

    Wiggins Guest

  3. #3

    Default Re: Check on array return result



    Tham, Philip wrote:
     

    It seems you're probably missing what is actualy happening there. (with
    all the globbing and po()ing ;p) Try this:

    for(check_vip_ports) {
    print "\$_ is $_";
    }
    # just use $_ instead of $token

    # or if you *really* want to use $token:
    for my $token(check_vip_ports) {
    print "\$token is $token";
    }

    You may want to
    perldoc -f pop
    perldoc perlop
    perldoc -f glob

    HTH :)
    Lee.M - JupiterHost.Net 
    Jupiterhost.Net Guest

  4. #4

    Default Re: Check on array return result

    Greetings;

    I keep getting a warning "Use of uninitialized value in
    string ..." and I can't find a way to eliminate it.

    Here is my code and the latest effort.

    foreach (<FD>) {
    chomp;
    s/^ *//;
    ($rank, $artist) = split(/ /, $_, 2); [/ref]
    $r{$artist} = $r{$artist} + $rank;
    $cnt{$artist} = $cnt{$artist} + 1;
    $name{$artist} = $artist;
    print qq($rank, $artist), "\n";
    print qq($r{$artist}, $cnt{$artist}, $name{$artist}), "\n";
    }

    The line marked >> is my latest attempt at solving the
    problem. While this does appear to work it still spits out a
    warning message for the first record containing a different
    $artist. That is what I would like to get rid of, but
    programmatically, not be deleting "use warnings;"

    I have a large file of "ballots" containing the artist's
    name and the ranking assigned, 1-500. I want to aculate
    the ranking and count the number of occurrences for each
    unique artist to calculate their average ranking and
    popularity.

    And help appreciated.

    Dennis
    Dennis Guest

  5. #5

    Default RE: Check on array return result

    Please group reply so others can help and be helped. Please bottom post.

     

    But do you need to be?
     

    The problem still has to do with the above use of the <>. I think you
    are getting weird results because of it.

     

    Are you counting on removing the elements from the array? Aka, are you
    then using the fact that you have popped off the valid ports from
    check_vip_ports later? How does $match_found used? Take a couple of
    steps back and tell us your overall plan and goal, we may be able to
    come up with a better solution, it appears you may want a grep/map/ or
    to use a hash.

    http://danconia.org
    Wiggins Guest

  6. #6

    Default Re: Check on array return result

    On Jun 1, Dennis G. Wicks said:
     

    Please start a new thread next time, instead of replying to a post and
    creating a new topic.
     

    Those two lines (the s/// and the split) could be rewritten as:

    ($rank, $artist) = split ' ', $_, 2;

    The ' ' argument to split() ignores leading and trailing whitespace.
     [/ref][/ref]

    Simply do:

    if (not exists $name{$artist}) { ... }

    The reason you're getting the error is, when the key $artist doesn't exist
    in %name, $name{$artist} returns undef, and using undef in a comparison
    like '==' or 'eq' gives you the warning message you described.

    Using the exists() function is more appropriate here.
     

    $r{$artist} += $rank;
     

    ++$cnt{$artist};
     

    Why don't you include the \n in the qq() strings? Or, why do you feel the
    need to use qq() here when a double-quoted string is fine?
     

    You might want to start including 'use strict' in your code; be aware,
    though, that this will require you to declare your variables. Please read
    variable scoping doentation to avoid headaches and frustration.

    --
    Jeff "japhy" Pinyan com http://www.pobox.com/~japhy/
    RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
    CPAN ID: PINYAN [Need a programmer? If you like my work, let me know.]
    <stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.

    Jeff Guest

  7. #7

    Default Unitialized variable; was Re: Check on array return result

    On Tue, 1 Jun 2004, Jeff 'japhy' Pinyan wrote:
     
    >
    > Please start a new thread next time, instead of replying to a post and
    > creating a new topic.[/ref]

    OK, so I ed up. Mea culpa!
     
    >
    > Those two lines (the s/// and the split) could be rewritten as:
    >
    > ($rank, $artist) = split ' ', $_, 2;
    >
    > The ' ' argument to split() ignores leading and trailing whitespace.[/ref]

    Thanks for the tip. I develop stepwise. The s/^ *//; came
    about because I encountered some records that had leading
    spaces, and right now "If it works, don't f*** with it!". [/ref]
    >
    > Simply do:
    >
    > if (not exists $name{$artist}) { ... }[/ref]

    Alright! That's what I was looking for! Works great!
     
    >
    > $r{$artist} += $rank;

    >
    > ++$cnt{$artist};[/ref]
    Yeah, I originally had code like you suggest, but I was
    getting the bad error messages and I thought I could fool it
    somehow.
     
    >
    > Why don't you include the \n in the qq() strings? Or, why do you feel the
    > need to use qq() here when a double-quoted string is fine?

    >
    > You might want to start including 'use strict' in your code; be aware,
    > though, that this will require you to declare your variables. Please read
    > variable scoping doentation to avoid headaches and frustration.[/ref]
    I do use ... this is just a snippet and the headers,

    #!/usr/bin/perl
    use diagnostics;
    use warnings;
    use strict;

    are way above the code in question.

    Thanks for the help and the tips!
    Dennis 
    Dennis Guest

Similar Threads

  1. Replies: 13
    Last Post: December 26th, 10:47 AM
  2. multilined regexp won't return result
    By perl@mortesicura.de in forum PERL Beginners
    Replies: 1
    Last Post: November 14th, 03:34 PM
  3. How to get php.exe to return a correct result code
    By Chris John Jordan in forum PHP Development
    Replies: 1
    Last Post: September 20th, 12:16 PM
  4. Return sp's result row into local variables?
    By Aaron in forum Microsoft SQL / MS SQL Server
    Replies: 3
    Last Post: July 17th, 02:50 PM
  5. Making PHP return SQL result to a different page
    By David Walker in forum PHP Development
    Replies: 4
    Last Post: June 30th, 09:19 AM

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