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.
In reply to Re: Unable to extract elements from a split. Kindly Help.
by GrandFather
in thread Unable to extract elements from a split. Kindly Help.
by perl514
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |