Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Unable to extract elements from a split. Kindly Help.

by perl514 (Pilgrim)
on Dec 24, 2011 at 04:22 UTC ( #945012=perlquestion: print w/replies, xml ) Need Help??

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

Venerated Monks,

I am trying to split a command output in an array and then print one specific line of it. Also trying to further split each line in words and count the occurances. For the second part of counting occurences, I will be referring the excellent node Count and List Items in a List. But before I do that, I need to break down the command as shown above. Kindly note that the "ping localhost" command is only being used as an example. I need to run some other commands and am using this only as an example. I wanted to give a sample output of those commands here, but to do that, I'd need to login to my office network and my remote connectivity to office VPN is disabled and will be resolved only by next 3-4 days. :(

I am getting an error message that states "Use of uninitialized value $eachlinecmd[4] in concatenation (.) or string at split.pl line 9." and "Use of uninitialized value $eachwordcmd[3] in concatenation (.) or string at split.pl line 10." Given below is my script

#!/usr/bin/perl use warnings; use strict; my @eachlinecmd = split ('/\n/,', `ping 127.0.0.1`); my @eachwordcmd = split ('/\w/,', @eachlinecmd); print "@eachlinecmd\n"; print "@eachwordcmd\n"; print "$eachlinecmd[4]\n"; print "$eachwordcmd[3]\n";

And here is the output

C:\Users\perl514\Documents\perl\practice>perl split.pl Pinging 127.0.0.1 with 32 bytes of data: Reply from 127.0.0.1: bytes=32 time<1ms TTL=64 Reply from 127.0.0.1: bytes=32 time<1ms TTL=64 Reply from 127.0.0.1: bytes=32 time<1ms TTL=64 Reply from 127.0.0.1: bytes=32 time<1ms TTL=64 Ping statistics for 127.0.0.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms 1 Use of uninitialized value $eachlinecmd[4] in concatenation (.) or str +ing at split.pl line 9. Use of uninitialized value $eachwordcmd[3] in concatenation (.) or str +ing at split.pl line 10.

Kindly help

Perlpetually Indebted To PerlMonks

Replies are listed 'Best First'.
Re: Unable to extract elements from a split. Kindly Help.
by GrandFather (Saint) on Dec 24, 2011 at 05:04 UTC

    The first major problem with your code is that the regular expression you use in the split is wrapped in single quotes so the match you are using is not at all the match you expect! The next issue is that you seem to want to deal with a number of lines that have been split into an array, but the array is in scalar context in the split so you get the count of elements as the string to be split instead of each line. But even fixing that won't do what you want. As a starting point consider the following code:

    #!/usr/bin/perl use warnings; use strict; my $str = <<TICKSTR; Pinging 127.0.0.1 with 32 bytes of data: Reply from 127.0.0.1: bytes=32 time<1ms TTL=64 Reply from 127.0.0.1: bytes=32 time<1ms TTL=64 Reply from 127.0.0.1: bytes=32 time<1ms TTL=64 Reply from 127.0.0.1: bytes=32 time<1ms TTL=64 Ping statistics for 127.0.0.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms TICKSTR for my $line (split /\n/, $str) { next if $line !~ /^Reply/; my @parts = split / /, $line; print "$_\n" for @parts[4, 5]; }

    Prints:

    time<1ms TTL=64 time<1ms TTL=64 time<1ms TTL=64 time<1ms TTL=64

    $str contains the result you might get using my $str = `ping 127.0.0.1`. The first split extracts the lines and the for loop loops over them. We are only interested in the "Reply" lines so we skip everything else. The second split breaks reply lines up into parts and the final statement prints out the two parts of the line of interest.

    True laziness is hard work
Re: Unable to extract elements from a split. Kindly Help.
by chromatic (Archbishop) on Dec 24, 2011 at 05:05 UTC

    Get rid of the single quotes around the regular expressions used as the first operand to split.

    I know you said that ping is just an example, but backticks in list context should give you an array of lines of output anyway, so you probably don't even need the first split. I'm not sure what you hope to accomplish with the second; if you remove the single quotes, it'll split on every word character, and you're not capturing them. Perhaps my @eachwordcmd = split ' ', $line; would work better?


    Improve your skills with Modern Perl: the free book.

Re: Unable to extract elements from a split. Kindly Help.
by Generoso (Prior) on Dec 24, 2011 at 05:31 UTC

    Since you do not say what is the expected result, my answer is guessing you want the element 3 from line 4.

    Try this:

    #!/usr/bin/perl use warnings; use strict; my @eachlinecmd = `ping 127.0.0.1`; my @eachwordcmd = split (/ /, $eachlinecmd[4]); print "@eachlinecmd\n"; print "@eachwordcmd\n"; print "$eachlinecmd[4]\n"; print "$eachwordcmd[3]\n";
    perl "F:\perl_TK\perl1ping.pl" Process started >>> Pinging 127.0.0.1 with 32 bytes of data: Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Ping statistics for 127.0.0.1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 0ms, Maximum = 0ms, Average = 0ms Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 Reply from 127.0.0.1: bytes=32 time<1ms TTL=128 bytes=32 <<< Process finished. ================ READY ================
Re: Unable to extract elements from a split. Kindly Help.
by cavac (Priest) on Dec 24, 2011 at 10:24 UTC

    GrandFather has already answered the question regarding splitting. I just want to add, for this specific tasks you might try to use Net::Ping.

    BREW /very/strong/coffee HTTP/1.1
    Host: goodmorning.example.com
    
    418 I'm a teapot
Re: Unable to extract elements from a split. Kindly Help.
by perl514 (Pilgrim) on Dec 24, 2011 at 14:32 UTC

    Hi Cavac,

    aargh.....My Bad. I should have first read the document that was pointed to by GrandFather. Now I understand. Whatever I pass to the split () function as EXPR, will be taken in a string or a scalar context. Got it...Sorry once again.

    Perlpetually Indebted To PerlMonks

      Just a little note: PerlMonks supports threading and reply-notification. So, when you respond someone, click the "Reply"-Link on the comment you are replying to. This makes for an easier read and also notifies the person you are replying to.

      BREW /very/strong/coffee HTTP/1.1
      Host: goodmorning.example.com
      
      418 I'm a teapot

        Hi Cavac,

        Sure....Will do :)

        Perlpetually Indebted To PerlMonks

Re: Unable to extract elements from a split. Kindly Help.
by perl514 (Pilgrim) on Dec 24, 2011 at 07:18 UTC

    Hi Honoured GrandFather,

    I tried your code but removed the <<TICKSTR with the actual command and the output is the same as yours. Thanks for the pointer.I didn't understand it when you said "but the array is in scalar context in the split". So I tried the following code:

    #!/usr/bin/perl use warnings; use strict; my $test = "this is a test and hope this will work.\nif it doesnt, the +n its ok.\n"; my @array = split (/\n/, $test); print "$array[0]\n"; my @splitarray = split (/ /, @array); print "@splitarray\n";

    And the output was:

    this is a test and hope this will work. 2

    So this means that, eventhough I mention my @splitarray = split (/ /, <b>@array</b>);, the @array is still taken in a scalar context. So that means whatever I give inside the bracket in the split function is taken in a scalar context? I am confused on this one. Kindly guide me to some pointer that will clarify this confusion.

    Perlpetually Indebted To PerlMonks

Re: Unable to extract elements from a split. Kindly Help.
by perl514 (Pilgrim) on Dec 24, 2011 at 06:28 UTC

    Venerated Monks,

    Wow....thank you all for taking time to reply. Let me work on this some more.

    Perlpetually Indebted To PerlMonks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://945012]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2022-05-21 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (77 votes). Check out past polls.

    Notices?