Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Trying to capture a value from a list of values

by parthodas (Acolyte)
on Sep 02, 2015 at 08:48 UTC ( #1140761=perlquestion: print w/replies, xml ) Need Help??

parthodas has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to capture the first value from a list of values. My script has multiple sub sections. In one, we have declared global variables and then as per requirement we are using them. As per my requirement, I'm calling linux servers(which has 4 values), and try to capture the first value. It is not coming as expected.
@mylnxsrvrs = split(',', $myhash{SBLLNXSRVRHOSTS}); @mywinsrvrs = split(',', $myhash{SBLWINSRVRHOSTS}); @mysblroot = split(',', $myhash{SIEBELROOT}); @mysblsrvrpath = (',',$myhash{SBLSRVRPATH}); foreach $a (@mylnxsrvrs) {my $lnxsrvr = "$a"; printf "$a $lnxsrvr\n\n";} printf "$lnxsrvr\n"; printf "@mylnxsrvrs";

The output looks like this --
dvapoms1 dvapoms1
dvapoms2 dvapoms2

dvapoms1 dvapoms2

If I'm trying to assign the value, it is giving junk result --
my $lnxsrvrs = "@mylnxsrvrs"; printf "$lnxsrvrs";
The output shows -- 1

Replies are listed 'Best First'.
Re: Trying to capture a value from a list of values
by flexvault (Monsignor) on Sep 02, 2015 at 10:40 UTC

    parthodas,

    Here's how I would like to see your script: ( YMMV )

    use strict; . . . my @mylnxsrvrs = split(',', $myhash{SBLLNXSRVRHOSTS}); foreach my $something ( @mylnxsrvrs ) { my $lnxsrvr = "$something"; print "$something $lnxsrvr\n\n"; } print "$lnxsrvr\n"; ## Undefined now! print "@mylnxsrvrs";
    Save the 'printf' for formatted printing, so that it becomes obvious upon review. And if you had used structures, you would have known that '$lnxsrvr' is undefined when you exit the 'foreach' loop. Perl allows for generous whitespace, so use whitespace to help you follow the flow of your work. Originally when I looked at your code, I didn't notice the closure to the 'foreach' loop.

    But I suspect your problem is in the 'split' and without seeing what '$myhash{SBLLNXSRVRHOSTS})' looks like, we can't help solve your problem.

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

      '$myhash{SBLLNXSRVRHOSTS})' has server names like -- dvapoms1,dvapoms2
      The code shows below things --
      @mylnxsrvrs = split(',', $myhash{SBLLNXSRVRHOSTS}); @mywinsrvrs = split(',', $myhash{SBLWINSRVRHOSTS}); @mysblroot = split(',', $myhash{SIEBELROOT}); @mysblsrvrpath = (',',$myhash{SBLSRVRPATH}); foreach $something (@mylnxsrvrs) {my $lnxsrvr = "$something"; printf "$something $lnxsrvr\n\n";} ##dvapoms1 dvapoms1 ##dvapoms2 dvapoms2 printf "$lnxsrvr\n"; ##"shows null" printf "@mylnxsrvrs"; ##dvapoms1 dvapoms2
      Hope this clears the point. I need to catch dvapoms1 in $lnxsrvr.
        Why have you completely ignored Ed's advice?

        To get the first value of your array:
        my $first_value = $mylnxsrvrs[0];
Re: Trying to capture a value from a list of values
by GotToBTru (Prior) on Sep 02, 2015 at 13:04 UTC

    Your example output could not come from the code you show: there would be a blank line after the first line of output. It is hard to help when you confuse things like that.

    Dum Spiro Spero
Re: Trying to capture a value from a list of values
by Laurent_R (Canon) on Sep 02, 2015 at 11:48 UTC
    my $lnxsrvrs = "@mylnxsrvrs";
    This will assign to $lnxsrvrs the number of elements of the @mylnxsrvrs array.

    But if this array has only one element, the it means that your split did not yield two fields as you seem to expect. Are you sure your fields are comma-separated? From your own OP, it might be space-separated values.

    Please check what you really have and clarify.

    Update: : forget what I crossed out, I had not paid attention to the quotes, which are changing everything. Still, plus specify your input: in one case it appears to be comma-separated, and in another post, space separated.

      my $lnxsrvrs = "@mylnxsrvrs";
      This will assign to $lnxsrvrs the number of elements of the @mylnxsrvrs array.

      Actually, it won't.

      Since @mylnxsrvrs is in double quotes, the contents of the array are concatenated and stringified, and this string is assigned to $lnxsrvrs.

      Consider:

      my @array = ( qw/ un deux trois / ); my $count = "@array"; print $count;

      Output:

      un deux trois

      How the OP (thinks they) got an output of 1 remains a mystery...

        Oh course, you're right, Not_a_Number ++, silly mistake on my part, I actually saw it myself when reviewing my post right before reading your answer. I answered to fast and I had not paid attention to the quotes.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1140761]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2020-10-22 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (225 votes). Check out past polls.

    Notices?