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

Re^2: Counting devices types, device sizes and number of devices.

by perl514 (Pilgrim)
on Jan 03, 2012 at 07:36 UTC ( #946012=note: print w/ replies, xml ) Need Help??


in reply to Re: Counting devices types, device sizes and number of devices.
in thread Counting devices types, device sizes and number of devices.

Hi Sewi,

Thanks for the reply.

I was planning to first use an array and then pass the elements of the array to a hash...Came out with very very basic stuff like this:

#!/usr/bin/perl use warnings; use strict; my $command = `symdev -sid 1234 list -noport -noreserve|find /I "not v +isible"`; my @cmdline = split (/\n/, $command); my @chgcmdline = @cmdline[5,8]; print "@chgcmdline\n";

But got stuck after this. Tried going through the node I have mentioned earlier and figuring out. The  symdev -sid 1234 list -noport -noreserve|find /I "not visible" is the command that gives the output mentioned in my post.

BTW - I am using Strawberry Perl that came bundled with Padre IDE and I do sometimes use Padre. Awesome IDE. Mr. Gabor did a great thing by pre bundling a lot of modules. Neat stuff :)

Perlpetually Indebted To PerlMonks


Comment on Re^2: Counting devices types, device sizes and number of devices.
Select or Download Code
Re^3: Counting devices types, device sizes and number of devices.
by Sewi (Friar) on Jan 03, 2012 at 20:03 UTC

    The my $command = `symdev... doesn't do what you might think. It puts back one line of the symdev output in $command. If you split one line by \n, one item remains (the line without line ending), so your 5,8 are empty/undef because the array simply doesn't have that many items.

    Try this instead:

    open my $fh, '-|', 'symdev -sid 1234 list -noport -noreserve|find /I " +not visible"'; while (<$fh>) { chomp; my @line = split(/\t/); [...]

    The sample above runs the symdev (I'm a little bit unsure about the piping 3-argument-open, try out open my $fh, 'symdev -sid 1234 list -noport -noreserve|find /I "not visible" |'; (notice the final | after the command if it doesn't work) and offers it's output via filehandle $fh.

    The while loop reads the output line by line presenting every single line in the default variable $_ for one loop run.

    Read chomp's documentation to see what it does. split also uses the default variable if none specified and returns the columns of the line.

    I suggested a text hash key instead of the hash tree shown in another answer to your original post because you simple don't need the detailed data in your sample and keeping things simpler might be better for the moment.

Re^3: Counting devices types, device sizes and number of devices.
by Marshall (Prior) on Jan 06, 2012 at 17:26 UTC
    Another tip for you...With Perl, you can specify indicies from either side of an array or list. Some of your data lines have 8 some 9 things. So to get the 6th thing and the last thing, do it like this.. Main point is that by using negative index, you can get the last thing even if number of things varies. I think your actual data is tab separated? if so, then adjust accordingly.
    while (<DATA>) { my ($desc,$MB) = (split)[5,-1]; print "$desc $MB\n"; } =prints 2-Way 3 2-Way 3 2-Way 3 RAID-5 8632 RAID-5 8632 RAID-5 34526 2-Way 34526 2-Way 34526 2-Way 69052 RAID-5 34526 RAID-5 34526 RAID-5 69052 RAID-5 69052 BCV+R5 34526 BCV+R5 34526
    The index of -1 means "last item", -2 would mean next to last one, etc.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946012]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (12)
As of 2014-09-19 20:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (145 votes), past polls