in reply to Re^12: Join multiple lines in a string based on regex.
in thread Join multiple lines in a string based on regex.

Remember this and this?

Its easy to learn to think like a computer if you use a piece of paper to walk through the program, through each iteration of the loop, and mark the changes to variables at each step

You should do this if you want to be able to solve this problem ... my teacher did this when he started, I did this when I started ... I think everybody does this when they start programming, otherwise they give up because its too hard when you cannot keep track of variable changes

Do you want to do this? Or do you want me to simply post the code I wrote?

  • Comment on Re^13: Join multiple lines in a string based on regex.

Replies are listed 'Best First'.
Re^14: Join multiple lines in a string based on regex.
by pr33 (Scribe) on Dec 24, 2014 at 04:46 UTC
    Thanks for the suggestion . I just started with learning perl , But I am in need for this due to time crunch . I would definitely improve with my ideas in the future . If you could post me your code , That will be helpful .

      If you could post me your code , That will be helpful.

      I hope it will be helpful :)

      #!/usr/bin/perl -- ## hpacucli-something-fun.pl ## ## ## 2014-12-23-18:40:42 first step ## ## ## ## perltidy -olq -csc -csci=3 -cscl="sub : BEGIN END if while " -otr +-opr -ce -nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; Main( @ARGV ); exit( 0 ); sub Main { my %Map; my $cmd = "hpacucli ctrl slot=0 pd all show"; #~ http://perlmonks.com/?abspart=1;displaytype=displaycode;node_id=111 +0713;part=2 my $data = q{ Smart Array P410i in Slot 0 (Embedded) array A physicaldrive 2C:1:1 (port 2C:box 1:bay 1, SAS, 1 TB, OK) physicaldrive 2C:1:2 (port 2C:box 1:bay 2, SAS, 1 TB, OK) array B physicaldrive 2C:1:3 (port 2C:box 1:bay 3, SAS, 1 TB, OK) physicaldrive 2C:1:4 (port 2C:box 1:bay 4, SAS, 1 TB, OK) array C physicaldrive 3C:1:5 (port 3C:box 1:bay 5, SAS, 1 TB, OK) physicaldrive 3C:1:6 (port 3C:box 1:bay 6, SAS, 1 TB, OK) array D physicaldrive 3C:1:7 (port 3C:box 1:bay 7, SAS, 1 TB, OK) physicaldrive 3C:1:8 (port 3C:box 1:bay 8, SAS, 1 TB, OK) array E physicaldrive 4C:2:1 (port 4C:box 2:bay 1, SAS, 1 TB, OK) physicaldrive 4C:2:2 (port 4C:box 2:bay 2, SAS, 1 TB, OK) array F physicaldrive 4C:2:3 (port 4C:box 2:bay 3, SAS, 1 TB, OK) physicaldrive 4C:2:4 (port 4C:box 2:bay 4, SAS, 1 TB, OK) array G physicaldrive 5C:2:5 (port 5C:box 2:bay 5, SAS, 1 TB, OK) physicaldrive 5C:2:6 (port 5C:box 2:bay 6, SAS, 1 TB, OK) array H physicaldrive 5C:2:7 (port 5C:box 2:bay 7, SAS, 1 TB, OK) physicaldrive 5C:2:8 (port 5C:box 2:bay 8, SAS, 1 TB, OK) array I physicaldrive 6C:3:1 (port 6C:box 3:bay 1, SAS, 1 TB, OK) physicaldrive 6C:3:2 (port 6C:box 3:bay 2, SAS, 1 TB, OK) array J physicaldrive 6C:3:3 (port 6C:box 3:bay 3, SAS, 1 TB, OK) physicaldrive 6C:3:4 (port 6C:box 3:bay 4, SAS, 1 TB, OK) array K physicaldrive 7C:3:5 (port 7C:box 3:bay 5, SAS, 1 TB, OK) physicaldrive 7C:3:6 (port 7C:box 3:bay 6, SAS, 1 TB, OK) unassigned physicaldrive 7C:3:7 (port 7C:box 3:bay 7, SAS, 1 TB, OK) physicaldrive 7C:3:8 (port 7C:box 3:bay 8, SAS, 1 TB, OK) }; ## open my $fd, "$cmd|" or die $!; open my $fd, '<', \$data or die $!; FillMap( $fd, \%Map ); dd( \%Map ); } ## end sub Main sub GetName { my( $name ) = $_[0] =~ m{^\s+(?:array)?\s(\S+)}; return $name; } ## end sub GetName sub FillMap { my( $fd, $MapRef ) = @_; my $name = ""; while( my $line = <$fd> ) { $line =~ /^\s*$/ and next; ## skip empty/whitespace if( $line =~ /^\S/ ) { ## skip header next; } elsif( $line =~ /^\s\s\s\S/ ) { ## array NAME my( $newName ) = GetName( $line ); $name = $newName; next; } elsif( $line =~ /^\s\s\s\s\s\s\S/ ) { ## array VALUE push @{ $MapRef->{$name} }, $line; next; } } ## end while( my $line = <$fd> ) close $fd; return $MapRef; } ## end sub FillMap __END__ { A => [ " physicaldrive 2C:1:1 (port 2C:box 1:bay 1, SAS, 1 TB, OK)\n +", " physicaldrive 2C:1:2 (port 2C:box 1:bay 2, SAS, 1 TB, OK)\n +", ], B => [ " physicaldrive 2C:1:3 (port 2C:box 1:bay 3, SAS, 1 TB, OK)\n +", " physicaldrive 2C:1:4 (port 2C:box 1:bay 4, SAS, 1 TB, OK)\n +", ], C => [ " physicaldrive 3C:1:5 (port 3C:box 1:bay 5, SAS, 1 TB, OK)\n +", " physicaldrive 3C:1:6 (port 3C:box 1:bay 6, SAS, 1 TB, OK)\n +", ], D => [ " physicaldrive 3C:1:7 (port 3C:box 1:bay 7, SAS, 1 TB, OK)\n +", " physicaldrive 3C:1:8 (port 3C:box 1:bay 8, SAS, 1 TB, OK)\n +", ], E => [ " physicaldrive 4C:2:1 (port 4C:box 2:bay 1, SAS, 1 TB, OK)\n +", " physicaldrive 4C:2:2 (port 4C:box 2:bay 2, SAS, 1 TB, OK)\n +", ], F => [ " physicaldrive 4C:2:3 (port 4C:box 2:bay 3, SAS, 1 TB, OK)\n +", " physicaldrive 4C:2:4 (port 4C:box 2:bay 4, SAS, 1 TB, OK)\n +", ], G => [ " physicaldrive 5C:2:5 (port 5C:box 2:bay 5, SAS, 1 TB, OK)\n +", " physicaldrive 5C:2:6 (port 5C:box 2:bay 6, SAS, 1 TB, OK)\n +", ], H => [ " physicaldrive 5C:2:7 (port 5C:box 2:bay 7, SAS, 1 TB, OK)\n +", " physicaldrive 5C:2:8 (port 5C:box 2:bay 8, SAS, 1 TB, OK)\n +", ], I => [ " physicaldrive 6C:3:1 (port 6C:box 3:bay 1, SAS, 1 TB, OK)\n +", " physicaldrive 6C:3:2 (port 6C:box 3:bay 2, SAS, 1 TB, OK)\n +", ], J => [ " physicaldrive 6C:3:3 (port 6C:box 3:bay 3, SAS, 1 TB, OK)\n +", " physicaldrive 6C:3:4 (port 6C:box 3:bay 4, SAS, 1 TB, OK)\n +", ], K => [ " physicaldrive 7C:3:5 (port 7C:box 3:bay 5, SAS, 1 TB, OK)\n +", " physicaldrive 7C:3:6 (port 7C:box 3:bay 6, SAS, 1 TB, OK)\n +", ], unassigned => [ " physicaldrive 7C:3:7 (port 7C:box 3:bay 7, SAS, 1 TB, OK)\n +", " physicaldrive 7C:3:8 (port 7C:box 3:bay 8, SAS, 1 TB, OK)\n +", ], }