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

Perl monks, will you provide some needed assistance please?
__DATA__ Disk Path P Location adapter LUN SN Type Size LSS Vol Rank C/A +S Connection port RaidMode ------- ----- - ----------- ------ ----------- ---------- +-- ---- ---- --- ----- ---- - ----------- ---- -------- vpath0 hdisk2 1n-08-01[FC] fscsi0 00013772 IBM 2105-F20 15.3GB 16 +0 1000 01 Y R1-B2-H4-ZA 2c RAID5 __END_DATA__
My code needs to delete the P column since its not populated and in the above example is after hdisk#. I tried using tr and s///, but of course it deletes all of the Ps
use strict; use warnings; use Data::Dumper; my ($vpath,$hdisk,$lun,$lunsz); my (%HoA,$HoA); open( SANCLI, "datapath query essmap |" ) or die "unable to open pipe. +.. $!"; while(<SANCLI>) { s/^\s+|\s+$//g; s/P{1,1}//g; next unless length $_; my @array=split; { local $" = "\t"; my @fields=@array[0,1,4,7]; print "@fields\n"; } #push @{$HoA{$id}}, $name; }

Replies are listed 'Best First'.
Re: splicing time
by graff (Chancellor) on Oct 03, 2007 at 03:47 UTC
    You posted a set of "data" that has three lines, and it's hard to know how to interpret that. (How many fields are there, really? Why is it that the groups of hyphens on the second line do not match the widths of fields on the other lines?)

    Meanwhile, the script is reading the output from some other process. (Is the data sample the exact output of that process? If not, it would be better to show us that output.)

    If the problem is that the first line of output from the other process (the column headings?) has more fields than any of the other lines, why even worry about it? Just skip that line:

    open( SANCLI, "datapath query essmap |" ) or die "unable to open pipe. +.. $!"; $_ = <SANCLI>; # read first line, do nothing with it while(<SANCLI>) { ...
    If you actually need to keep the column headings (e.g. to use them as hash keys later), just remove the "P" column from that one line before splitting it:
    open( SANCLI, "datapath query essmap |" ) or die "unable to open pipe. +.. $!"; $_ = <SANCLI>; s/\s+P\s+/ /; my @col_headings = split; while(<SANCLI>) { my %fields; @fields{@col_headings} = split; ... }
    If I'm not answering your question, please try to be more clear about what the problem is.
Re: splicing time
by GrandFather (Saint) on Oct 03, 2007 at 01:43 UTC

    Are those columns supposed to be tab aligned? They are not aligned using space padding. If tab aligned, where do the tabs go? Perhaps you could use | characters in place of tabs to make it a little clearer?


    Perl is environmentally friendly - it saves trees
      well thats a good question! And thats what I tried to look for, tabs or any other special char, using cat -etu "file" I will try |. yes I could accept that the field will be there, but it annoys and I know awk can do it. thank you to both!
Re: splicing time
by bruceb3 (Pilgrim) on Oct 03, 2007 at 01:43 UTC
    Why don't you just accept that the P column is going to be there and adjust the elements of the @fields array that you are use? Don't worry about getting rid of it the column, just ignore it after you've split the line.