Q: how to push into array

2008-01-09 Thread ciwei
HI, a quetion from a newbie to perl.
I have a program that extract some fields:

#!/usr/bin/perl -w

use strict;

my ( $host, $hba, $storage, $fa ,$initiator , $target ) ;
my %wwn = ();

while (  ) {
 next unless /\s+([A-Z]\w+)[_-](HBA\d)[_-]([A-Z]\w+)[_-](\w+)\s+ ...
WWN: \d\d\d\d\w{12}/;
( $host, $hba, $storage, $fa ) = /\s+([A-Z]\w+)[_-](HBA\d)[_-]([A-Z]\w
+)[_-](\w+)\s+/;

   print "HOST = $host , HBA = $hba , storage = $storage, FA = $fa
\n";

   $initiator = $host."-".$hba;
   $target  = $storage."-".$fa;

   push  $wwn{$initiator}, $1   if /WWN:\s+(1000\d{12})/;
   push  $wwn{$target}   , $1   if /WWN:\s+(500\d{13})/;

}

__DATA__
 DEV01-HBA0_DMX1-13CA
  WWN: 1000C934A35B
  WWN: 5006048ACAFE1E4C
 DEV01_HBA0_CX-SPA_P0
  WWN: 1000C934A35B
  WWN: 500601601060007E
 TEST01_HBA1_STK_TAPES1
  WWN: 10E002239270
  WWN: 500104F000619193
  WWN: 500104F00061918D
  WWN: 500104F000619190
  WWN: 500104F00061919D

when I run it ,  I got

#emc_parse_switch_zone.pl
Type of arg 1 to push must be array (not hash elem) at ./
emc_parse_switch_zone.pl line 20, near "$1 if"
Execution of ./emc_parse_switch_zone.pl aborted due to compilation
errors.

so my questions:
1. what is wrong with push here?
2. how to properly determine the boundaris of the records.  each
record ending with the last "WWN: " lnes.


thanks.
ciwei


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Q: how to push into array

2008-01-10 Thread ciwei
Thanks for answering my question.
> I don't understand your second question, could you break your input
> into records so we can clearly see what how you want to define the
> records?

My data looks like this: the rcord is variable in the number of
lines.
I'm tring to push all WWN that start with 1000 to initiator array.
and all others into target array.
Thanks.


__DATA__
 DEV01-HBA0_DMX1-13CA  <-- begin of record
  WWN: 1000C934A35B
  WWN: 5006048ACAFE1E4C <-- end of record
  EST01_HBA1_STK_TAPES1 <-- begin of record
  WWN: 10E002239270
  WWN: 500104F000619193
  WWN: 500104F00061918D
  WWN: 500104F000619190
  WWN: 500104F00061919D <-- end of record.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Q: how to push into array

2008-01-10 Thread ciwei

> push @{ $wwn{ "$host-$hba"   } }, /^\s+WWN:\s+(1000[0-9a-fA-F]{12})$/;

Thanks for the help.

can you please explain in the above line , what the { } around
push @{ $wwn ...  } <--here do ?

is the { } here optional?  or can this be subsitute with ( )?
thanks.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Q: how to push into array

2008-01-12 Thread ciwei
thanks for the reply.  I was not able to figure out how the code
works.  I have commented the code below.  can you help
explain it a bit?  thanks.



On Jan 10, 9:41 am, [EMAIL PROTECTED] (Chas. Owens) wrote:
> > __DATA__
> >              DEV01-HBA0_DMX1-13CA  <-- begin of record
> >               WWN: 1000C934A35B
> >               WWN: 5006048ACAFE1E4C <-- end of record
> >               EST01_HBA1_STK_TAPES1 <-- begin of record
> >               WWN: 10E002239270
> >               WWN: 500104F000619193
> >               WWN: 500104F00061918D
> >               WWN: 500104F000619190
> >               WWN: 500104F00061919D <-- end of record.
>
> snip
>
> my $record = <>; #prime the pump

this suck in the whole input file into $record. a scaler.
Is that what you really mean?  or how do you read in
just the first line within  the file?

> while (my $line = <>) {
>     if ($line =~ /^\s*WWN/) {
>         $record .= $line;

this append the $line to $record, which contain the whole file
earlier ?
what we want to achive here?

>         next;
>     }

so we exit the loop unless if match WWN.

and we start porceesing the record for lines that don't matched
above?

>     process_record($record);
>     $record = $line; #start a new record}

I didn't see how this start a record record, $line contain
the


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




space in variable referencing

2008-01-12 Thread ciwei
Question from a newbie: how to properly use space
when reference to variables, array, hash etc.

please see  the code below:
why the first line, second, and third line all output differiently.
thanks.
ciwei

code below ==
 #!/usr/bin/perl

my %ttys =();
open ( WHO, "who|") or die "can;t \n";

while (  ){

( $user, $tty ) = split ;
 push @{$ttys{ $user}} , $tty;

}

foreach  $user ( sort keys %ttys ){

print "first line : $user => @{$ttys {$user}} \n";
print "second line: $user => @ {$ttys { $user}} \n";
print "third line:  $user => @{$ttys{$user}} \n";

}

__OUTPUT__
first line : root =>
second line: root => @ { { root}}
third line:  root => pts/1 pts/4 pts/6 pts/7 pts/8


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




order of command line switch?

2008-02-13 Thread ciwei
hostA>ls
SUNWjassVRTSVRTSicsco   VRTSvcs
emc SUNWmlibVRTSalloc   VRTSjre VRTSvlicVRTSvxvm

hostA>ls  | perl -en 'print if /SUNW/'

return nothing , while

hostA>ls  | perl -ne 'print if /SUNW/'
SUNWits
SUNWjass
SUNWmlib
SUNWrtvc

so why the order of  -n -e switch make the differience?
this is perl 5.8.4.
Thanks


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Q: how to rename file with space in filename

2008-03-20 Thread ciwei

# ls -l
-rw-r--r--   1 root other  0 Mar 19 16:24 this is the first file
-rw-r--r--   1 root other  0 Mar 19 16:24 this is the second file
-rw-r--r--   1 root other  0 Mar 19 16:24 this is the third file

I want to rename the files, to say, first, second, etc

# ls -1 | perl -ne 'print $1,"\n" if /(\w+)\s+file$/'
first
second
third

now try use rename
# ls -1 | perl -ne 'rename $_ ,  $1  if /(\w+)\s+file$/'

won't work, like nothing happed?  why?

Thanks in advance.

ciwei


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




how to repeat non-atom patterns

2008-03-27 Thread ciwei
Given a multiple char patterns like ":C9"  that repeated, how to write
a regex that repeat the patterns( which is non-atom )  6 times.  like
in
below

   WWPN:10:00:00:00:c9:2e:e8:90

I tried to define pattern to match

my $match= qr/ {:[0-9a-e][0-9a-e]}{6} /;
print matched if /$match/ ;

but it unable to match.  any suggestions.

Thanks.
ciwei


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/