Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

splitting the scalar variables in an array

by sunil9009 (Acolyte)
on Nov 16, 2013 at 05:17 UTC ( #1062852=perlquestion: print w/replies, xml ) Need Help??
sunil9009 has asked for the wisdom of the Perl Monks concerning the following question:

I have a list of hostclasses each having a list of host, I want to combine all the hosts in the hostclasses to give me an array of hosts. Command "expand-hostclass" gives me a list of hosts with each host as a newline. My problem is that the array considers hosts in each hostclass as a scalar ($hosts_per_hostclass).I want to flatten the scalar ($hosts_per_hostclass) to an array, so that each element in the array is a hostname. Hope I am not confusing too much Please help. I tried the following code

#!/usr/bin/perl my @hostclass = ('Hostclass-A','Hostclass-B','Hostclass-C'); my @hosts; my @TotalHosts; my $class; my $hosts_per_hostclass; gethosts(); sub gethosts { foreach $class(@hostclass) { $hosts_per_hostclass = `expand-hostclass` ; push (@TotalHosts,$hosts_per_hostclass); my @dd = split "\n", @TotalHosts; print "@dd \n"; } }

Replies are listed 'Best First'.
Re: splitting the scalar variables in an array
by Athanasius (Chancellor) on Nov 16, 2013 at 06:55 UTC

    split expects its second argument to be a string (i.e., a scalar), so using the array @TotalHosts here puts the array into scalar context, and split gets the number of elements in the array. You need to change this line:

    my @dd = split "\n", @TotalHosts;

    to something like this:

    my @dd; push @dd, split "\n", $_ for @TotalHosts;

    which calls split on each element of the array in turn. This works provided that each of the array elements is a string.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      my @dd; push @dd, split "\n", $_ for @TotalHosts;

      No need for push and for:

      my @dd=map { split "\n" } @TotalHosts;


      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Thanks for the help, I modified the code but that didnt helped either. The scalar value for @dd returned by the code is as follows 1 1 4 Thats because Hostclass-A has 1 host, Hostclass B has 1 and C has 4 hosts. I want scalar(@dd) to be just 6 ( each host should be a separate element in an array )

      sub gethosts { foreach $class(@hostclass) { chomp (@hosts_per_hostclass = `/opt/systems/bin/expand-hostclass --hos +ts -r $class`) ; my @dd; push @dd, split "\n", $_ for @hosts_per_hostclass; print scalar(@dd) , "\n"; } }

        Consider this resolved, foreach was printing it 3 times. Final working code is as follows. Thanks to all of you guys fo the pointers.

        sub gethosts { foreach $class(@hostclass) { chomp (@hosts_per_hostclass = `/opt/systems/bin/expand-hostclass --hos +ts -r $class`) ; #print "@hosts_per_hostclass \n"; push @dd, split "\n", $_ for @hosts_per_hostclass; } return @dd; }
Re: splitting the scalar variables in an array
by jwkrahn (Monsignor) on Nov 16, 2013 at 10:36 UTC
    $hosts_per_hostclass = `/opt/systems/bin/expand-hostclass --hosts -r $ +class` ; push (@TotalHosts,$hosts_per_hostclass);

    Back-quotes in list context return a list so you could do this:

    chomp( @hosts_per_hostclass = `/opt/systems/bin/expand-hostclass --hos +ts -r $class` ); push @TotalHosts, @hosts_per_hostclass;
Re: splitting the scalar variables in an array
by soonix (Monsignor) on Nov 16, 2013 at 08:14 UTC
    I would move the split and print out of the loop.
    Update: wasn't yet awake... push @TotalHosts, split ... IN the loop, and print, of course, after the loop.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1062852]
Approved by Athanasius
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2018-04-22 23:12 GMT
Find Nodes?
    Voting Booth?