Re: How can I do this in Perl?

2011-04-01 Thread Wernher Eksteen
> that * isn't doing what you think it does. perl regexes are not shell
> globs. it happens to work anyway since the tokens are unique
> enough. read perlretut to learn perl regexes.
>
> what have you tried so far? you know enough perl to get the array of
> lines and loop over that. in english (or your native tongue), just
> describe how you would do it to get to your goal. write it down. it is
> only a few steps. then it will be easier to convert that to the perl
> needed to accomplish it. if you then have more troubles, post your code
> here and you will get help. a clue: you need a hash and its values will
> be array references. read perlreftut, perllol and perldsc for more on
> that.
>
> uri

Thanks Uri, I will take this into consideration and learn from this
going forward
and also make sure I understand what Rob's answer actually does and compare
notes and reference with the likes of perltut etc.

Kind regards,
Wernher

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How can I do this in Perl?

2011-04-01 Thread Wernher Eksteen
> Please always 'use strict' and 'use warnings', and consequently declare
> all of your variables. That way most straightforward problems will be
> solved my Perl before ever reaching his list.

Thanks, I will remember to do so for future.

> It is better to open a pipe to a child process running your command,
> rather than read all the output into an array and process that. I
> suggest something like the program below, which builds a hash of the
> device/disk relationship and prints it out at the end.
>
> HTH,
>
> Rob

Thanks Rob, this works fantastic!

Regards,
Wernher Eksteen

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How can I do this in Perl?

2011-04-01 Thread Rob Dixon

On 01/04/2011 19:52, Wernher Eksteen wrote:


 From the folowing list is a result of the @power array, when run
through the foreach loop:

Pseudo name=emcpowerd
   1 lpfc  sdba SP A7 active  alive  0  0
   1 lpfc  sddd SP B7 active  alive  0  0
   3 lpfc  sdfg SP B6 active  alive  0  0
   3 lpfc  sdhj SP A6 active  alive  0  0
Pseudo name=emcpowerc
   1 lpfc  sdbb SP A7 active  alive  0  0
   1 lpfc  sdde SP B7 active  alive  0  0
   3 lpfc  sdfh SP B6 active  alive  0  0
   3 lpfc  sdhk SP A6 active  alive  0  0
Pseudo name=emcpoweraz
   1 lpfc  sdbh SP B7 active  alive  0  0
   3 lpfc  sddk SP B6 active  alive  0  0
   1 lpfc  sde  SP A7 active  alive  0  0
   3 lpfc  sdfn SP A6 active  alive  0  0

 From the list above, how can Perl assign the sd* disks to it's
relevant emcpower device, so that the output shows this:

emcpowerdsdba sddd sdfg sdhj
emcpowercsdbb sdde sdfh sdhk
emcpoweraz  sdbh sddk sde sdfn

This is how the @power array was obtained:

$powermt = 'powermt display dev=all';
@power = `$powermt`;

foreach my $i (@power) {
if (($i =~ /emcpower*/) || ($i =~ /lpfc*/)) {
print $i;
}
}


Please always 'use strict' and 'use warnings', and consequently declare
all of your variables. That way most straightforward problems will be
solved my Perl before ever reaching his list.

It is better to open a pipe to a child process running your command,
rather than read all the output into an array and process that. I
suggest something like the program below, which builds a hash of the
device/disk relationship and prints it out at the end.

HTH,

Rob


use strict;
use warnings;

my $powermt = 'powermt display dev=all';

open my $fh, '-|', $powermt or die $!;

my ($device, %disks);

while (my $line = <$fh>) {

  if ( $line =~ /name=(emcpower\w+)/ ) {
$device = $1;
  }
  elsif ( $line =~ /\blpfc\s+(sd\w+)/ ) {
push @{$disks{$device}}, $1;
  }
}

foreach my $device (sort keys %disks) {
  printf "%s  %s\n", $device, join ' ', @{$disks{$device}};
}

** OUTPUT **

emcpoweraz  sdbh sddk sde sdfn
emcpowerc  sdbb sdde sdfh sdhk
emcpowerd  sdba sddd sdfg sdhj

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: How can I do this in Perl?

2011-04-01 Thread Uri Guttman
> "WE" == Wernher Eksteen  writes:


  WE> Pseudo name=emcpowerd
  WE>   1 lpfc  sdba SP A7 active  alive  0 
 0
  WE>   1 lpfc  sddd SP B7 active  alive  0 
 0
  WE>   3 lpfc  sdfg SP B6 active  alive  0 
 0
  WE>   3 lpfc  sdhj SP A6 active  alive  0 
 0
  WE> Pseudo name=emcpowerc
  WE>   1 lpfc  sdbb SP A7 active  alive  0 
 0
  WE>   1 lpfc  sdde SP B7 active  alive  0 
 0
  WE>   3 lpfc  sdfh SP B6 active  alive  0 
 0
  WE>   3 lpfc  sdhk SP A6 active  alive  0 
 0
  WE> Pseudo name=emcpoweraz
  WE>   1 lpfc  sdbh SP B7 active  alive  0 
 0
  WE>   3 lpfc  sddk SP B6 active  alive  0 
 0
  WE>   1 lpfc  sde  SP A7 active  alive  0 
 0
  WE>   3 lpfc  sdfn SP A6 active  alive  0 
 0

  >>> From the list above, how can Perl assign the sd* disks to it's
  WE> relevant emcpower device, so that the output shows this:

  WE> emcpowerdsdba sddd sdfg sdhj
  WE> emcpowercsdbb sdde sdfh sdhk
  WE> emcpoweraz  sdbh sddk sde sdfn

  WE> This is how the @power array was obtained:

  WE> $powermt = 'powermt display dev=all';
  WE> @power = `$powermt`;

  WE> foreach my $i (@power) {
  WE>if (($i =~ /emcpower*/) || ($i =~ /lpfc*/)) {

that * isn't doing what you think it does. perl regexes are not shell
globs. it happens to work anyway since the tokens are unique
enough. read perlretut to learn perl regexes.

what have you tried so far? you know enough perl to get the array of
lines and loop over that. in english (or your native tongue), just
describe how you would do it to get to your goal. write it down. it is
only a few steps. then it will be easier to convert that to the perl
needed to accomplish it. if you then have more troubles, post your code
here and you will get help. a clue: you need a hash and its values will
be array references. read perlreftut, perllol and perldsc for more on
that.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




How can I do this in Perl?

2011-04-01 Thread Wernher Eksteen
Hi,

>From the folowing list is a result of the @power array, when run
through the foreach loop:

Pseudo name=emcpowerd
  1 lpfc  sdba SP A7 active  alive  0  0
  1 lpfc  sddd SP B7 active  alive  0  0
  3 lpfc  sdfg SP B6 active  alive  0  0
  3 lpfc  sdhj SP A6 active  alive  0  0
Pseudo name=emcpowerc
  1 lpfc  sdbb SP A7 active  alive  0  0
  1 lpfc  sdde SP B7 active  alive  0  0
  3 lpfc  sdfh SP B6 active  alive  0  0
  3 lpfc  sdhk SP A6 active  alive  0  0
Pseudo name=emcpoweraz
  1 lpfc  sdbh SP B7 active  alive  0  0
  3 lpfc  sddk SP B6 active  alive  0  0
  1 lpfc  sde  SP A7 active  alive  0  0
  3 lpfc  sdfn SP A6 active  alive  0  0

>From the list above, how can Perl assign the sd* disks to it's
relevant emcpower device, so that the output shows this:

emcpowerdsdba sddd sdfg sdhj
emcpowercsdbb sdde sdfh sdhk
emcpoweraz  sdbh sddk sde sdfn

This is how the @power array was obtained:

$powermt = 'powermt display dev=all';
@power = `$powermt`;

foreach my $i (@power) {
   if (($i =~ /emcpower*/) || ($i =~ /lpfc*/)) {
   print $i;
   }
}

Thanks,
Wernher

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: parsing a delimited file

2011-04-01 Thread Rob Dixon
On 01/04/2011 13:57, Rob Dixon wrote:
>
> use strict;
> use warnings;
> 
> my $data;
> {
>local $/;
>$data =;
> }
> 
> my $key = qr/ (?: \w+ [ ] )* \w+ /x;
> 
> my @xx = $data =~ /\G ( $key ) \s? : \s* (.+?) \s* (?=  $key \s? : | \z)/sgx;

My apologies, I had thought that there was always exactly one space
between the end of the key and the colon. In fact the field 'PM NODE
NUMBER' differs from all the others. The fix is to change this line to

  my @xx = $data =~ /\G ( $key ) \s* : \s* (.+?) \s* (?=  $key \s* : | \z)/sgx;

> 
> while (@xx) {
>my ($key, $val) = splice @xx, 0, 2;
>printf "%s=%s\n", $key, $val;
> }

Rob

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: parsing a delimited file

2011-04-01 Thread Rob Dixon
On 01/04/2011 12:00, Anirban Adhikary wrote:
> Hi List
> 
> I am trying to parse the following file and load it into a hash.
> 
> File Structure
> 
> 
[snip data]
> 
> Separater is colon(:)
> 
> my code
> 
> use strict;
> use warnings;
> my $fname = $ARGV[0];
> open my $RFH,'<',$fname;
> 
> while (<$RFH>) {
>   next unless /:/ and  my %field = /(.*?):(.*?)/mg;
>   foreach my $key (keys %field ) {
>   print "KEY=[$key]--VALUE=[$field{$key}]\n";
>   }
> }
> 
> But I am not getting the desired result.

The code below seems to do what you want.

Cheers,

Rob


use strict;
use warnings;

my $data;
{
  local $/;
  $data = ;
}

my $key = qr/ (?: \w+ [ ] )* \w+ /x;

my @xx = $data =~ /\G ( $key ) \s? : \s* (.+?) \s* (?=  $key \s? : | \z)/sgx;

while (@xx) {
  my ($key, $val) = splice @xx, 0, 2;
  printf "%s=%s\n", $key, $val;
}

__DATA__
DN: 2570764   (PORTED-IN)
TYPE: SINGLE PARTY LINE
SNPA: 438   SIG: DTLNATTIDX: 4
XLAPLAN KEY :   438CAUC1RATEAREA KEY : ILEPEROT
LINE EQUIPMENT NUMBER: LG31 5 02 57
LINE CLASS CODE:  1FR
IBN TYPE: STATION
CUSTGRP: RESGRP SUBGRP: 0  NCOS: 1
LINE TREATMENT GROUP: 1
CARDCODE:  RDTLSGGND: N  PADGRP: PKNIL  BNV: NL MNO: N
PM NODE NUMBER :387
PM TERMINAL NUMBER :258
DNGRPS OPTIONS:
NETNAME: PUBLIC
ADDRESS:  5144254676
OPTIONS:
COD DGT NAME PUBLIC HR BLOCK IMPOTS PIC 6885 Y
RES OPTIONS: NONE
OFFICE OPTIONS:
U3WC AIN TIID

**OUTPUT**

DN=2570764   (PORTED-IN)
TYPE=SINGLE PARTY LINE
SNPA=438
SIG=DT
LNATTIDX=4
XLAPLAN KEY=438CAUC1
RATEAREA KEY=ILEPEROT
LINE EQUIPMENT NUMBER=LG31 5 02 57
LINE CLASS CODE=1FR
IBN TYPE=STATION
CUSTGRP=RESGRP
SUBGRP=0
NCOS=1
LINE TREATMENT GROUP=1
CARDCODE=RDTLSG
GND=N
PADGRP=PKNIL
BNV=N
L MNO=N
PM NODE NUMBER :387
PM TERMINAL NUMBER=258
DNGRPS OPTIONS=N
ETNAME=PUBLIC
ADDRESS=5144254676
OPTIONS=COD DGT NAME PUBLIC HR BLOCK IMPOTS PIC 6885 Y
RES OPTIONS=NONE
OFFICE OPTIONS=U3WC AIN TIID

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: parsing a delimited file

2011-04-01 Thread John W. Krahn

Anirban Adhikary wrote:

I want to create a new file where I get each line like KEY=VALUE
In the source file in a single line the are multiple KEY-VALUE pair
and there is also some line where KEY VALUE pair spreads across two
lines.


That's nice.

Please do not top-post your replies.  And please trim any unwanted text 
in your replies.  Thank you (in advance.)


And again: what exactly are the desired results?



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: parsing a delimited file

2011-04-01 Thread Anirban Adhikary
I want to create a new file where I get each line like KEY=VALUE
In the source file in a single line the are multiple KEY-VALUE pair
and there is also some line where KEY VALUE pair spreads across two
lines.

On Fri, Apr 1, 2011 at 4:49 PM, John W. Krahn  wrote:
> Anirban Adhikary wrote:
>>
>> Hi List
>
> Hello,
>
>> I am trying to parse the following file and load it into a hash.
>>
>> File Structure
>>
>>
>> DN:     2570764                   (PORTED-IN)
>> TYPE: SINGLE PARTY LINE
>> SNPA: 438   SIG: DT    LNATTIDX: 4
>> XLAPLAN KEY :   438CAUC1                RATEAREA KEY :     ILEPEROT
>> LINE EQUIPMENT NUMBER:     LG    31 5 02 57
>> LINE CLASS CODE:      1FR
>> IBN TYPE: STATION
>> CUSTGRP:         RESGRP     SUBGRP: 0  NCOS: 1
>> LINE TREATMENT GROUP:     1
>> CARDCODE:  RDTLSG    GND: N  PADGRP: PKNIL  BNV: NL MNO: N
>> PM NODE NUMBER     :    387
>> PM TERMINAL NUMBER :    258
>> DNGRPS OPTIONS:
>> NETNAME: PUBLIC
>> ADDRESS:              5144254676
>> OPTIONS:
>> COD DGT NAME PUBLIC HR BLOCK IMPOTS PIC 6885 Y
>> RES OPTIONS: NONE
>> OFFICE OPTIONS:
>> U3WC AIN TIID
>>
>> Separater is colon(:)
>>
>> my code
>>
>> use strict;
>> use warnings;
>> my $fname = $ARGV[0];
>> open my $RFH,'<',$fname;
>
> You should always verify that the file opened correctly before trying to use
> the filehandle:
>
> open my $RFH, '<', $fname or die "Cannot open '$fname' because: $!";
>
>
>> while (<$RFH>) {
>>        next unless /:/ and  my %field = /(.*?):(.*?)/mg;
>
> You are testing for the presence of a colon twice which is redundant.
>
> In the match /(.*?):(.*?)/mg the /m option is superfluous because you are
> not using the anchors ^ or $.  But your real problem seems to be ':(.*?)'
> which will match *zero* non-newline characters after the colon.  In lines
> with more than one colon you need some way to properly parse the different
> fields which may be difficult because it looks like some keys have embedded
> whitespace.
>
>
>>        foreach my $key (keys %field ) {
>>                print "KEY=[$key]--VALUE=[$field{$key}]\n";
>>        }
>> }
>>
>> But I am not getting the desired result.
>
> What exactly are the desired results?
>
>
>
> John
> --
> Any intelligent fool can make things bigger and
> more complex... It takes a touch of genius -
> and a lot of courage to move in the opposite
> direction.                   -- Albert Einstein
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: parsing a delimited file

2011-04-01 Thread John W. Krahn

Anirban Adhikary wrote:

Hi List


Hello,


I am trying to parse the following file and load it into a hash.

File Structure


DN: 2570764   (PORTED-IN)
TYPE: SINGLE PARTY LINE
SNPA: 438   SIG: DTLNATTIDX: 4
XLAPLAN KEY :   438CAUC1RATEAREA KEY : ILEPEROT
LINE EQUIPMENT NUMBER: LG31 5 02 57
LINE CLASS CODE:  1FR
IBN TYPE: STATION
CUSTGRP: RESGRP SUBGRP: 0  NCOS: 1
LINE TREATMENT GROUP: 1
CARDCODE:  RDTLSGGND: N  PADGRP: PKNIL  BNV: NL MNO: N
PM NODE NUMBER :387
PM TERMINAL NUMBER :258
DNGRPS OPTIONS:
NETNAME: PUBLIC
ADDRESS:  5144254676
OPTIONS:
COD DGT NAME PUBLIC HR BLOCK IMPOTS PIC 6885 Y
RES OPTIONS: NONE
OFFICE OPTIONS:
U3WC AIN TIID

Separater is colon(:)

my code

use strict;
use warnings;
my $fname = $ARGV[0];
open my $RFH,'<',$fname;


You should always verify that the file opened correctly before trying to 
use the filehandle:


open my $RFH, '<', $fname or die "Cannot open '$fname' because: $!";



while (<$RFH>) {
next unless /:/ and  my %field = /(.*?):(.*?)/mg;


You are testing for the presence of a colon twice which is redundant.

In the match /(.*?):(.*?)/mg the /m option is superfluous because you 
are not using the anchors ^ or $.  But your real problem seems to be 
':(.*?)' which will match *zero* non-newline characters after the colon. 
 In lines with more than one colon you need some way to properly parse 
the different fields which may be difficult because it looks like some 
keys have embedded whitespace.




foreach my $key (keys %field ) {
print "KEY=[$key]--VALUE=[$field{$key}]\n";
}
}

But I am not getting the desired result.


What exactly are the desired results?



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: parsing a delimited file

2011-04-01 Thread Anirban Adhikary
Have u run your code?\
Here u cant chomp because you have key/value pairs spans more than one
line..

Thanks & Regards
Anirban.

On Fri, Apr 1, 2011 at 4:37 PM, Ramprasad Prasad wrote:

> See Inline comments
>
> use strict;
>> use warnings;
>> my $fname = $ARGV[0];
>> open my $RFH,'<',$fname;
>>
>> while (<$RFH>) {
>>next unless /:/ and  my %field = /(.*?):(.*?)/mg;  # Wrong
>>
> # I would prefer doing this
> chomp;
>if(/^(.+?)\:(.+?)/){ $field{$1}=$2;}
> }
>
>
>
>
>>foreach my $key (keys %field ) {
>>print "KEY=[$key]--VALUE=[$field{$key}]\n";
>>}
>> }
>>
>> But I am not getting the desired result.
>>
>> Thanks & Regards in advance
>> Anirban Adhikary.
>>
>> --
>> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
>> For additional commands, e-mail: beginners-h...@perl.org
>> http://learn.perl.org/
>>
>>
>>
>
>
> --
> Thanks
> Ram
>   
>
>
>
>
> n 
>
>


Re: parsing a delimited file

2011-04-01 Thread Ramprasad Prasad
See Inline comments

use strict;
> use warnings;
> my $fname = $ARGV[0];
> open my $RFH,'<',$fname;
>
> while (<$RFH>) {
>next unless /:/ and  my %field = /(.*?):(.*?)/mg;  # Wrong
>
# I would prefer doing this
chomp;
   if(/^(.+?)\:(.+?)/){ $field{$1}=$2;}
}




>foreach my $key (keys %field ) {
>print "KEY=[$key]--VALUE=[$field{$key}]\n";
>}
> }
>
> But I am not getting the desired result.
>
> Thanks & Regards in advance
> Anirban Adhikary.
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>


-- 
Thanks
Ram
  




n 


parsing a delimited file

2011-04-01 Thread Anirban Adhikary
Hi List

I am trying to parse the following file and load it into a hash.

File Structure


DN: 2570764   (PORTED-IN)
TYPE: SINGLE PARTY LINE
SNPA: 438   SIG: DTLNATTIDX: 4
XLAPLAN KEY :   438CAUC1RATEAREA KEY : ILEPEROT
LINE EQUIPMENT NUMBER: LG31 5 02 57
LINE CLASS CODE:  1FR
IBN TYPE: STATION
CUSTGRP: RESGRP SUBGRP: 0  NCOS: 1
LINE TREATMENT GROUP: 1
CARDCODE:  RDTLSGGND: N  PADGRP: PKNIL  BNV: NL MNO: N
PM NODE NUMBER :387
PM TERMINAL NUMBER :258
DNGRPS OPTIONS:
NETNAME: PUBLIC
ADDRESS:  5144254676
OPTIONS:
COD DGT NAME PUBLIC HR BLOCK IMPOTS PIC 6885 Y
RES OPTIONS: NONE
OFFICE OPTIONS:
U3WC AIN TIID

Separater is colon(:)

my code

use strict;
use warnings;
my $fname = $ARGV[0];
open my $RFH,'<',$fname;

while (<$RFH>) {
next unless /:/ and  my %field = /(.*?):(.*?)/mg;
foreach my $key (keys %field ) {
print "KEY=[$key]--VALUE=[$field{$key}]\n";
}
}

But I am not getting the desired result.

Thanks & Regards in advance
Anirban Adhikary.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: perl dd

2011-04-01 Thread Shlomi Fish
On Thursday 31 Mar 2011 16:54:14 Peter Scott wrote:
> On Thu, 31 Mar 2011 12:07:41 +0200, Shlomi Fish wrote:
> > Also, Larry Wall has allowed Perl 5 numeric constants to contain
> > underscore so you can write 10**9 as 1_000_000_000 instead of 10
> > which is much less readable and also more error-prone. (Larry Wall)++ .
> 
> 1E9 seems to capture your intention even better :-)

Heh. :-). just for the record:

1. 1_000_000_000 was just for the sake of the example. It could also be:
2_435_279_145 or something else that is arbitrary.

2. The problem with 1E9 is that I think it's a floating point number (though 
Perl 5 may have some intelligence there). Not sure it matterns a lot for Perl 
but if you do something like 1.27E9 you may get weird side-effects due to:

http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html

(Oracle++ for fixing the old link from docs.sun.com after it was broken.)

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
UNIX Fortune Cookies - http://www.shlomifish.org/humour/fortunes/

  Khisanth =~ s/must sleep/must give Botje all my money/ .
-- Freenode's #perl

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/