-----Original Message-----
From: Uri Guttman [mailto:u...@stemsystems.com] 
Sent: Wednesday, April 21, 2010 4:21 PM
To: Lonnie Ellis
Cc: beginners@perl.org
Subject: Re: Using backtick operator output and feeding it into array

>>>>> "LE" == Lonnie Ellis <lel...@claimspages.com> writes:

  LE> I'm pretty new to perl, and can't seem to get the following bit of
code
  LE> to work.  If I print the array while in the sub, it returns what I
want
  LE> but outside of the sub it doesn't recognize it.  Aren't all
variable
  LE> global by default unless told otherwise with "my"?  Here is code
below,
  LE> any help would be appreciated.

what do you doesn't recognize it? that is very vague. please show what
you get for output in both places and what you expect to see.

  LE> #!/usr/bin/perl

  LE>  use 5.010;

no need for that unless you are actually using 5.10 only features.

  LE>  use diagnostics;

  LE>  # use strict;

why is that commented out? it should be enabled for all your perl.

  LE>  use Data::Dumper;

  LE>  sub getDig {

  LE>     $net = "10.0.1";

  LE>     @num = (1..254);

  LE>     @digOutput = ();

well, you aren't declaring anything with scoping since you disabled
strict. so your variables are all globals which is BAD. fix that
immediately and you can then learn how to pass data into and out from
subs.
 

  LE>     foreach (@num) {

use named variables for loop control. better code and easier to read.

  LE>        $ipaddie = $net . ".$_";

        $ipaddie = "$net.$_";


  LE>        (@digOutput) = `dig -x $ipaddie +short`;

no need for the parens there. assigning to an array is in list context.

  LE>     }

  LE>     return \...@digoutput;

why are you returning a reference there? you can just return the array
of lines.

  LE>  }

 

  LE> my @c = getDig();

that will only have one element in it, an array ref.

  LE> my $dumped = Dumper(\...@c);

no need for the extra reference there as you already have an array
ref. if you returned the array itself, then that ref makes dumper work
the way you want.

  LE> print $dumped;

as we don't see the output you claim is wrong, we can't tell what needs
to be fixed. dumper should dump the output with an extra reference level
or two wrapping it. regardless, writing code like this without strict is
evil and you should learn to avoid it now. this code is simple enough
that you can clean in up in a few minutes.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------
http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support
------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com
---------
[LE] 
Thank you Uri for your help.  I know most of the code was jacked up from
me testing, but I appreciate your insight.  Linux Experts solution was
perfect.  As for the other stuff, I'm fixing it also.  What I was trying
to do was get an array that I can use for input into another portion of
code. Just the machine names that were returned with the dig statement.
I was wanting an array since it would be easiest to iterate over.
Thanks again.

-Lonnie


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to