Help - Search - Member List - Calendar
Full Version: combinations
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44
Support our Sponsors!
John Moon
Subject: 'format' puzzle

How can I create a perl 'format' string that begins with a comment (#)?

Eg.

format STDOUT_TOP =
# Field1 Field2
@<< @||| @|||
..


If I try '#' then I see the backslash. I just want the pound char.

TIA,
-Sam


I'm not sure where you want the "#" ... but

DEV,SUN2>more foo2
#! /usr/bin/perl
format STDOUT_TOP =
@ fld1 fld2
"#"
..
format STDOUT =
@<< @||| @|||
$a,$b,$c
..
$a = 1;
$b = 2;
$c = 3;
write;
DEV,SUN2>./foo2
# fld1 fld2
1 2 3
DEV,SUN2>

Sam
Thanks. Was right there in the manual... -Sam

--- "Moon, John" <[Email Removed]> wrote:
QUOTE
Subject: 'format' puzzle

How can I create a perl 'format' string that begins with a comment (#)?

Eg.

format STDOUT_TOP =
#  Field1  Field2
@<< @|||    @|||
.


If I try '#' then I see the backslash.  I just want the pound char.

TIA,
-Sam


I'm not sure where you want the "#" ... but

DEV,SUN2>more foo2
#! /usr/bin/perl
format STDOUT_TOP =
@  fld1  fld2
"#"
.
format STDOUT =
@<< @|||  @|||
$a,$b,$c
.
$a = 1;
$b = 2;
$c = 3;
write;
DEV,SUN2>./foo2
#  fld1  fld2
1    2      3
DEV,SUN2

--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response






__________________________________
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/

Offer Kaye
On 4/18/05, N. Ganesh Babu wrote:
QUOTE
Dear All

I want to do capitalisation of this text. I am using the following code.
It works fine with all other lines.

$line="Level A (Grade 1 reading level)";

@words=split(/ /,$line);
for($i=0;$i<=$#words;$i++)
{
$words[$i]=~s!($words[$i])!uL$1!gi;
print "$words[$i]n";
}


Hi Ganesh,
There is a Perl builtin function called "ucfirst" that does what you
want (read "perldoc -f ucfirst"), but I also wanted to point out that
your code is very "un-perlish". Specifically:

QUOTE
@words=split(/ /,$line);
for($i=0;$i<=$#words;$i++)

There is no need to keep the result of split in a temporary array
(@words), nor iterate over the array using a c-style "for" loop.
Simple iterate over the results of the split:
for (split /(s+)/, $line) { ...code... }
Inside the for loop (is actually a "foreach" loop, but the two
keywords are interchangable in Perl), the special variable $_ will
hold the current word returned by split.

If you look at my split usage, you will notice I wrote the pattern
"/(s+)/" inteead of "/ /". It is better to split of "one or more
whitespace" instead of on a "single space", incase your string might
include something like "word (two spaces) word". Read "perldoc -f
split" for details.
In addition, I return the whitespace as part of the results, so that
the string will not look different (in terms of whitespace) when I
print it out. Here is the complete code:
########## begin code
use strict;
use warnings;
my $line="Level A (Grade 1 reading level)";
my $ucline = "";
for (split /(s+)/,$line )
{
$ucline .= ucfirst($_);
}
print "$uclinen";
########## end code
Of course this means the ucfirst function will also operate on
whitespace, but this doesn't do any harm.

HTH,
--
Offer Kaye

Jay Savage
On 4/18/05, N. Ganesh Babu <[Email Removed]> wrote:
QUOTE
Dear All

I want to do capitalisation of this text. I am using the following code.
It works fine with all other lines.

$line="Level A (Grade 1 reading level)";

@words=split(/ /,$line);
for($i=0;$i<=$#words;$i++)
{
$words[$i]=~s!($words[$i])!uL$1!gi;
print "$words[$i]n";
}

This code is showing the following error: The main reason I understood
is the presence of (  and ) in the text.

perl test.pl
Level
A
Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE (Grade)/ at
test.pl line 6.

Compilation exited abnormally with code 255 at Mon Apr 18 15:22:33

Please help me in this regard to solve this problem.

Regards,
Ganesh

Ganesh,

The problem you're having here is because your double quoted string is
being interpolated.

$words[2] = "(Grade"

Therefore you regex turns into"

s/((Grade)/uL($1)/gi

But you don't need to interpolate the variable, $words[$i] =~
m/$words[$i]/ is redundant; it's given. The same goes for /g: yuo know
you're matching the ientire sting, the match can't possibly repeat.

Also, ($1) will add extra parentheses in the output.

What you're looking for is something like this:

foreach my $word (split / /, $line) {
$word =~ s/(.*)/uL$1/;
print "$word ";
}

or better yet, skip the loop all together:

$line =~ /(S+)/uL$1/g ;

This assumes, of course, that all you data is like your sample, and
the letter following the opening parenthesis is always capitalized.
Otherwise, it gets more complicated.

HTH,

--jay

Offer Kaye
On 4/18/05, Jay Savage wrote:
QUOTE

$line =~ /(S+)/uL$1/g ;


Almost right - returns "Level A (grade 1 Reading Level)" - notice the
lowercase "g" in "grade"). Should be:
$line =~ s/(w+)/u$1/g;

--
Offer Kaye

thundergnat
Offer Kaye wrote:
QUOTE
On 4/18/05, Jay Savage wrote:

$line =~ /(S+)/uL$1/g ;



Almost right - returns "Level A (grade 1 Reading Level)" - notice the
lowercase "g" in "grade"). Should be:
$line =~ s/(w+)/u$1/g;


Or, even more error resistant:

$line =~ s/(bw+'?w*b)/uL$1/g

Peter Farrar
On Mon, 18 Apr 2005, Anish Kumar K wrote:

QUOTE
Can [anyone] suggest a good algorithm for [encrypting] and
[decrypting] files ..I donot want to use any perl module for that...

Check out the perl stuff on this site. I haven't actually used it yet,
though I've been meaning to look at it. But their Java stuff is really
good.
http://www.cryptix.org/

Not sure why you don't want to use a module, but you could always pull out
the parts you need.



****** CONFIDENTIALITY NOTICE ******
NOTICE: This e-mail message and all attachments transmitted with it
may contain legally privileged and confidential information intended
solely for the use of the addressee. If the reader of this message is
not the intended recipient, you are hereby notified that any reading,
dissemination, distribution, copying, or other use of this message or its
attachments is strictly prohibited. If you have received this message
in error, please notify the sender immediately and delete this message
from your system. Thank you..

Siegfried Heintze wrote:
QUOTE
Hey Perlers!

This code used to work and then I changed something and it no longer
works! The problem is the output no longer includes "string 2a" and
"string 2b". What am I doing wrong?

How do explicitly assign an array (not an array reference, but an
array) to an array cell?

Thanks,
Sieg


sub concat {
# All arguments are assumed to be strings or nested arrays of
strings. # Concatenate all elements of all arguments.
return join "", map ref eq "ARRAY" ? concat( @$_ ) : $_, @_ }

my @sResult = ();
push @sResult,  "string 1";
push @sResult,  "string 2";

my $nMark = @sResult;

push @sResult, ();  # make an array of an array cell: does not work

push @sResult = "string 3";


$sResult[$nMark][@{$sResult[$nMark]}] = "string 2a";
$sResult[$nMark][@{$sResult[$nMark]}] = "string 2b";

If you want the array, then change above two lines:


$sResult[$nMark] = ["string 2a", "string 2b"] ;

But the way this is, the 'string 3' will be replaced. if want to keep the string 3, then I did a

if ( defined $sResult[$nMark] ) {
$sResult[$nMark] = [$sResult[$nMark], "string 2a", "string 2b"] ;

}else {
$sResult[$nMark] = ["string 2a", "string 2b"];
}

The output with this last one is:
string 1string 2string 3string 2astring 2b

But unsure is that what you are after.

Wags ;)
}> print concat @sResult;



*******************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
*******************************************************

Siegfried Heintze
See comments in line:

QUOTE
sub concat {
# All arguments are assumed to be strings or nested arrays of
strings. # Concatenate all elements of all arguments.
return join "", map ref eq "ARRAY" ? concat( @$_ ) : $_, @_ }

my @sResult = ();
push @sResult,  "string 1";
push @sResult,  "string 2";

my $nMark = @sResult;

push @sResult, ();  # make an array of an array cell: does not work

push @sResult = "string 3";


$sResult[$nMark][@{$sResult[$nMark]}] = "string 2a";
$sResult[$nMark][@{$sResult[$nMark]}] = "string 2b";

If you want the array, then change above two lines:

$sResult[$nMark] = ["string 2a", "string 2b"] ;

(1) Does $sResult[$nMark] contain an array reference or an array? I want
it to contain an array.


QUOTE

But the way this is, the 'string 3' will be replaced. if want to
keep the string 3, then I did a


(2) Why is this necessary? I did a push to create a gap. Why is not my
"push @sResult, ();" creating a gap?

QUOTE
if ( defined $sResult[$nMark] ) {
    $sResult[$nMark] = [$sResult[$nMark], "string 2a", "string 2b"] ;

  }else {
    $sResult[$nMark] = ["string 2a", "string 2b"];
}



Thanks!

Siegfried

Siegfried Heintze wrote:
QUOTE
See comments in line:

sub concat {
# All arguments are assumed to be strings or nested arrays of
strings. # Concatenate all elements of all arguments.
return join "", map ref eq "ARRAY" ? concat( @$_ ) : $_, @_ }

my @sResult = ();
push @sResult,  "string 1";
push @sResult,  "string 2";

my $nMark = @sResult;

push @sResult, ();  # make an array of an array cell: does not work

push @sResult = "string 3";


$sResult[$nMark][@{$sResult[$nMark]}] = "string 2a";
$sResult[$nMark][@{$sResult[$nMark]}] = "string 2b";

If you want the array, then change above two lines:

$sResult[$nMark] = ["string 2a", "string 2b"] ;

(1) Does $sResult[$nMark] contain an array reference or an array? I
want it to contain an array.

When you look at the output fo Data::Dumper:

$VAR1 = [
'string 1',
'string 2',
[
'string 3',
'string 2a',
'string 2b'
]
];
It is an array.

Wags ;)
QUOTE


But the way this is, the 'string 3' will be replaced. if want to
keep the string 3, then I did a


(2) Why is this necessary? I did a push to create a gap. Why is not
my "push @sResult, ();" creating a gap?

if ( defined $sResult[$nMark] ) {
    $sResult[$nMark] = [$sResult[$nMark], "string 2a", "string 2b"] ;

  }else {
    $sResult[$nMark] = ["string 2a", "string 2b"];      }



Thanks!

Siegfried



*******************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
*******************************************************

Jose Nyimi
QUOTE
-----Message d'origine-----
De : Graeme McLaren [mailto:[Email Removed]]
Envoy : dimanche 24 avril 2005 19:01
: [Email Removed]
Objet : basic class problem


Hi all I'm trying to write a basic class that will contain some
configuration details, unfortunately it bombs out and doesn't
do anything.

################### start of class
########################### #configuration.pm package
configuration; use strict;


sub new{
my $class=shift;
my $self=shift;

bless($self, $class);
return $self;
}

sub get_conf{
my $self=shift;
my %conf=(
db_name="bla1",
      username="bla2",
      password="bla3"

);

return %conf;

}

1;
################ end of class ####################


Mt CGI script is this:

##################### CGI Script #######################
#!/usr/bin/perl -w use strict; use configuration;

print "Content-type: text/htmlnn";

my $conf=configuration->new;
my $conf->get_conf;

print "$conf->{'db_name'}";

my $obj=configuration->new;
my $conf=$obj->get_conf;
print $conf->{'db_name'};#perldoc Data::Dumper BTW

HTH,
Jos.

Jeff 'japhy' Pinyan
On Apr 25, Larsen, Errin M HMMA/IT said:

QUOTE
$ perl -MO=Deparse -ane 'print pop(@F), "n";'

Note the @F, it's capital-F.

QUOTE
# perl -MO=Deparse -nae 'print $f[4]' /some/directory/somefile

You're using a lowercase @f here.

QUOTE
LINE: while (defined($_ = <ARGV>)) {
our(@F) = split(" ", $_, 0);
print $f[4];
}
-e syntax OK

Perl will magically produce the @F array for you, as shown. It's up to
YOU not to make the typo.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart

Robert Kerry
I changed the web.xml file and the .jar suffix as required, and create
a new directory WEB-INF/cgi/ under webapps/ directory, I put my
first.pl into that directory, and try to visit it using
http://localhost:8080/cgi/first.pl but it doesn't work. I've also
tried /cgi-bin/first.pl and something else, always give me HTTP 404
file not found error. What should I do? Thank you.

Robert.

Jason Normandin
Hello List.

Found the problem:

foreach my $record (@{dataFileHash{$datafile}}) {

Should have been:

foreach my $record (@{$dataFileHash{$datafile}}) {

Sorry ! I stared at this for like an hour and didn't notice the missing $....

Can anyone tell me what the Argument "NH_DATA01" isn't numeric in helem at ./nhsDiffSchema.pl line 236. error means though? It would be nice to know what a helem is in the future :)
QUOTE

From: <[Email Removed]
Date: 2005/04/25 Mon PM 07:29:29 GMT
To: <[Email Removed]
Subject: 'Bad index while coercing array into hash' error

Greetings

I am getting the following error when running a script using an anonymous array of hashes:

Bad index while coercing array into hash at ./nhsDiffSchema.pl line 240

Here is the code snipt that both assigns values to the array of hashes and the code which displays the values back (where the error is coming from) :

* I have a scalar named $dataFiles that contains info similar to the following:

export/spiderman1/ora_data_1/oradata/EHEALTH/SYSTEM01.dbf                                                                                                                                                                                                                                                                                                                                                                                                                                                                        SYSTEM                                    250                                  160.61                        89.39              1                        89.39                                35.76 YES

Here is the code:

my @dataFilesInfo=split /n/,$dataFiles;
foreach my $datafile (@dataFilesInfo) {
  next if $datafile =~ m/^$/;
  my @attributes=split /s+/,$datafile;
  push @{$dataFileHash{$attributes[0]}},{
  tableSpace => "$attributes[1]",
  allocMB => "$attributes[2]",
  usedMB => "$attributes[3]",
  freeMB => "$attributes[4]",
    numFrags => "$attributes[5]",
  maxFrag => "$attributes[6]",
  percentFree => "$attributes[7]",
  autoExtend => "$attributes[8]"
  };
}

oreach my $datafile (sort keys %dataFileHash) {
  print "datafile : $datafilen";
  foreach my $record (@{dataFileHash{$datafile}}) {
  print "tTablespace: $record->{tableSpace}n";
  print "tMB Allocated: $record->{allocMB}n";
  print "tMB Used: $record->{usedMB}n";
  print "tMB Free: $record->{freeMB}n";
  print "tNumber Frags: $record->{numFrags}n";
  print "tMax Frag: $record->{maxFrag}n";
  print "t% Free: $record->{percentFree}n";
  print "tAuto Extend: $record->{autoExtend}n";
  };
}

The error is coming from line 236, which is:

print "tTablespace: $record->{tableSpace}n";

If I comment out that line, the error then goes to 237. So, it would seem I am accessing the values incorrectley some how but I can't seem to figure it out.


I have used the Data::Dumper module to dump the contents of the array of hashes and it seems to be populated as I thought:

$VAR1 = '/export/spiderman1/ora_data_1/oradata/EHEALTH/SYSTEM01.dbf';
$VAR2 = [
{
'usedMB' => '160.61',
'autoExtend' => 'YES',
'freeMB' => '89.39',
'allocMB' => 250,
'percentFree' => '35.76',
'numFrags' => 1,
'tableSpace' => 'SYSTEM',
'maxFrag' => '89.39'
}
];
$VAR3 = '/export/spiderman1/ora_data_1/oradata/EHEALTH/NH_INDEX01.dbf';
$VAR4 = [
{
'usedMB' => '394.52',
'autoExtend' => 'YES',
'freeMB' => '86.48',
'allocMB' => 481,
'percentFree' => '17.98',
'numFrags' => 40,
'tableSpace' => 'NH_INDEX',
'maxFrag' => '57.13'
}
];

.....

I am utterly confused as I have similar code that works correctley:

my @tableInfo=split /n/,$allTables;
foreach my $table (@tableInfo) {
  my @attributes=split /s+/,$table;
  push @{$tables{$attributes[0]}},{
  table_space => "$attributes[1]",
  num_rows => "$attributes[2]"
  };
}

foreach my $table (sort keys %tables) {
  print "table:$tablen";
  foreach my $record (@{$tables{$table}}) {
  print "tTablespace=$record->{table_space}n";
  print "tNumber of rows=$record->{num_rows}nn";
  }
}

I am using strict and when I enable warnings, I see the following message:

Argument "NH_DATA01" isn't numeric in helem at ./nhsDiffSchema.pl line 236.

I am not sure what that means and what 'helem' refers too.

Help !!

Thanks :)
Jason


--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response




Robert Kerry
Chris,

Thanks. I'm not switching to Apache http server.

Unfortunately the same thing happened again. I put the
first.pl in the /cgi-bin/ directory and try to access it via
http://localhost/cgi-bin/first.pl, it tells me that file not found.
I don't know what I should do for httpd.conf file. Thanks.

Robert.


On 4/25/05, Chris Devers <[Email Removed]> wrote:
QUOTE
On 4/25/05, Robert Kerry <[Email Removed]> wrote:
I changed the web.xml file and the .jar suffix as required, and create
a new directory WEB-INF/cgi/ under webapps/ directory, I put my
first.pl into that directory, and try to visit it using
http://localhost:8080/cgi/first.pl but it doesn't work. I've also
tried /cgi-bin/first.pl and something else, always give me HTTP 404
file not found error. What should I do?

You mean, what should you do aside from hope that Perl will just
magically execute from within a *Java* application server ??

Maybe I'm misinformed, but Tomcat is as far as I know only for running
code written in Java (or weird hybrids like Jython).

If you want to run Perl, why aren't you running Apache/CGI or Apache/mod_perl?

--
Chris Devers


Offer Kaye
On 4/25/05, Robert Kerry wrote:
QUOTE
What should I do? Thank you.


You should use Google :-)
The first link returned by searching for "Tomcat" and "Perl":
http://www.ftponline.com/javapro/2003_03/o...teden_03_18_03/

HTH,
--
Offer Kaye

Chris Devers
On Fri, 1 Jan 1988, amr wrote:

QUOTE
Do we have Perl script that can edit & add to the Windows (XP, 2000) path.?

Is there a reason that a simple system() command won't work here?

my $status = system( "SET PATH=%PATH%;$new_path" );
die "Couldn't set %PATH%: $!" if $status;

Normally I'm opposed to using system commands when the task can be done
either all in Perl or with a platform- and problem- specific module, but
in this case, the cmd shell's SET command is easy to run directly, so
why not just solve the problem that way?


--
Chris Devers

Chris Devers
On Wed, 27 Apr 2005, Bakken, Luke wrote:

QUOTE
That will change the environment for the cmd.exe process that is
executed by that one system() call. Changing %ENV will change the
environment for all processes started by the perl script.

That's the behavior on POSIXy systems, but I had the impression that on
Windows the SET command would have a global effect.

I seem to remember using a variant of this to do things like set file
associations from the DOS shell on NT4, and being surprised that a
command I expected to only work for the current session having effects
on all future sessions. Maybe that was something else though...

But I avoid Windows as much as I can get away with, so I could easily be
wrong about this.



--
Chris Devers

yes I agree I was a little ambiguous... I was in a hurry. sorry. Anyway
here is my updated code. and here is a sample output:
My goal is to get all F01 which I am but I am having issues capturing all
of these values into my array. When I run the I get the data I want to see
which is just the F01 column, but when I comment out line 14 and uncomment
line 15 and uncomment line 21 I see no data in the array???
In the end I want the F01 column and the % column.

thank you,

derek

1 2005/01/20 15:39 17 2% -il-o-b- - - - - sg F01000
2 2005/01/20 15:53 14 1% -il-o-b----- sg F01001
3 2005/01/18 09:53 2 0% -il-o-b----- sg F01002
4 2005/02/04 16:41 196 100% -il-o-b----f sg F01003
5 2005/02/05 21:13 305 100% -il-o-b----f sg F01004
6 2005/02/28 22:47 180 100% -il-o-b----- sg F01005
13 2005/02/08 16:07 112 100% -il-o-b----f sg F01006
14 2005/02/09 21:56 122 100% -il-o-b----f sg F01007
15 2005/02/11 10:51 147 100% -il-o-b----f sg F01008
16 2005/02/13 11:35 193 100% -il-o-b----f sg F01009
17 2005/02/14 23:46 79 100% -il-o-b- - - -f sg F01010


#!/usr/bin/perl
1> use strict;
2> use warnings;
3> $ENV{"PATH"} =
qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);
4> open (V4, "samcmd v4 2>&1 |" ) || die "unable to open pipe... Broken?
$!";
5> my @fa =();
6> my @ha =();
7> my $i =0;
8> foreach (<V4>) {
9> local $, = "n";
10> #print +(split)[6,7], $,;
11> s <sg> ();
12> s {-*} ()g;
13> s {w+} ()i;
14> print +(split)[5,6,7], if (m/f01(d+)/gi )
15> #$fa[$i++] = +(split)[5,6,7] if (m/f01(d+)/gi );
16> #print +(split)[4],$,; #% column
17> }
18> close (V4);
19> print "n";
20> print "Now printing array element 0t", $fa[0], "n";
21> #print "Now printing entire array t", @fa, "n";
22> print "Now printing array count t", $#fa, "n";





Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams





"Ing. Branislav
Gerzo"
<[Email Removed]> To
[Email Removed]
04/27/2005 02:46 cc
AM
Subject
Re: REGEXP










[Email Removed] [D], on Tuesday, April 26, 2005 at 17:12
(-0400) made these points:

D> s/sg//, s/- {1,}(w{1,})//,print +(split)[5,6,7], if (m/f01(d+)/gi )

I think no one replied to you because we don't know what should be
output.

--

How do you protect mail on web? I use http://www.2pu.net

["I am a Jello's God!" shouted Yahweh, quivering in anger.]



--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

JupiterHost.Net
[Email Removed] wrote:
QUOTE
yes I agree I was a little ambiguous... I was in a hurry. sorry.  Anyway

Then do it at a later time and do it right, I imagine most all of us are
busy also :)

QUOTE
here is my updated code. and here is a sample output:
My goal is to get all F01 which I am but I am having issues capturing all


Then you're doing a rube goldberg if that is indeed what you're trying
to do:

# untested and prolly even more efficient way but it
# illustrates enhancements to the original code

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

$ENV{PATH} = '/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log';
open V4, 'samcmd v4 2>&1 |' or die "unable to open pipe: $!";

my @last_col;
for(<V4>) {
chomp;
my @tmp = split /w+/;
push @last_col, $tmp[ $#tmp ];
}

close V4;
print Dumper @last_col;

now @last_col has all of the F01... pieces of data.

QUOTE
of these values into my array.  When I run the I get the data I want to see
which is just the F01 column, but when I comment out line 14 and uncomment
line 15 and uncomment line 21 I see no data in the array???
In the end I want the F01 column and the % column.

thank you,

derek

1    2005/01/20 15:39  17  2%  -il-o-b- - - - -  sg F01000
2    2005/01/20 15:53  14  1%  -il-o-b-----  sg F01001
3    2005/01/18 09:53    2  0%  -il-o-b-----  sg F01002
4    2005/02/04 16:41  196 100%  -il-o-b----f  sg F01003
5    2005/02/05 21:13  305 100%  -il-o-b----f  sg F01004
6    2005/02/28 22:47  180 100%  -il-o-b-----  sg F01005
13    2005/02/08 16:07  112 100%  -il-o-b----f  sg F01006
14    2005/02/09 21:56  122 100%  -il-o-b----f  sg F01007
15    2005/02/11 10:51  147 100%  -il-o-b----f  sg F01008
16    2005/02/13 11:35  193 100%  -il-o-b----f  sg F01009
17    2005/02/14 23:46  79 100%  -il-o-b- - - -f  sg F01010


#!/usr/bin/perl
1> use strict;
2> use warnings;
3> $ENV{"PATH"} =
qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);
4> open (V4, "samcmd v4 2>&1 |" ) || die "unable to open pipe... Broken?
$!";

Whay double quote/qq() all of that when no quotes or single quotes are
better?
also
"or die" is more preferable than "|| die"

QUOTE
5> my @fa =();
6> my @ha =();


my @fa;
my @ha;

the = () isn't necessary and doesn't keep you from getting uninitialized
value warnings like you think it does, thats only scalars.

Offer Kaye
On 1/1/88, amr wrote:
QUOTE
Hi All,

Do we have Perl script that can edit & add to the Windows (XP, 2000) path..?


The PATH environment variable can be accessed using the %ENV hash:
print "$ENV{PATH}";

You can change $ENV{"PATH"} in the script and it will change the PATH
for the rest of the script (and any sub-processes launched by it).

Someone else will have to help you if you want to change the system's
PATH, I don't know how to do that. Perhaps you should clarify what you
meant.

--
Offer Kaye

Peter Rabbitson
QUOTE
also
"or die" is more preferable than "|| die"

Why is that? :) I was actually going to post a question about ambiguity
syntax later, but here it is anyway. Are the following 4 equivalent?

1)
if ($b) {
$a = $b;
}
else {
$a = $c;
}

2)
$a = $b or $c;

3)
$a = $b || $c;

4)
$a = $b ? $b : $c;


Also there was an example on the web that completely threw me off. Although
this works:

local ($/);

,I have no idea how it undefs $/. Points to a good reading on the
subject are equally appreciated.

Thanks

Peter

Jay Savage
On 4/27/05, [Email Removed] <[Email Removed]> wrote:
QUOTE
yes I agree I was a little ambiguous... I was in a hurry. sorry.  Anyway
here is my updated code. and here is a sample output:
My goal is to get all F01 which I am but I am having issues capturing all
of these values into my array.  When I run the I get the data I want to see
which is just the F01 column, but when I comment out line 14 and uncomment
line 15 and uncomment line 21 I see no data in the array???
In the end I want the F01 column and the % column.

thank you,

derek

1    2005/01/20 15:39  17  2%  -il-o-b- - - - -  sg F01000
2    2005/01/20 15:53  14  1%  -il-o-b-----  sg F01001
3    2005/01/18 09:53    2  0%  -il-o-b-----  sg F01002
4    2005/02/04 16:41  196 100%  -il-o-b----f  sg F01003
5    2005/02/05 21:13  305 100%  -il-o-b----f  sg F01004
6    2005/02/28 22:47  180 100%  -il-o-b-----  sg F01005
13    2005/02/08 16:07  112 100%  -il-o-b----f  sg F01006
14    2005/02/09 21:56  122 100%  -il-o-b----f  sg F01007
15    2005/02/11 10:51  147 100%  -il-o-b----f  sg F01008
16    2005/02/13 11:35  193 100%  -il-o-b----f  sg F01009
17    2005/02/14 23:46  79 100%  -il-o-b- - - -f  sg F01010

#!/usr/bin/perl
1> use strict;
2> use warnings;
3> $ENV{"PATH"} > qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);
4> open (V4, "samcmd v4 2>&1 |" ) || die "unable to open pipe... Broken?
$!";
5> my @fa =();
6> my @ha =();
7> my $i =0;
8>        foreach (<V4>) {
9>              local $, = "n";
10>              #print +(split)[6,7], $,;
11>              s <sg> ();
12>              s {-*} ()g;
13>                s {w+} ()i;
14>                print +(split)[5,6,7], if (m/f01(d+)/gi )
15>              #$fa[$i++] = +(split)[5,6,7] if (m/f01(d+)/gi );
16>              #print +(split)[4],$,; #% column
17>      }
18> close (V4);
19> print "n";
20> print "Now printing array element 0t", $fa[0], "n";
21> #print "Now printing entire array t", @fa, "n";
22> print "Now printing array count t", $#fa, "n";

Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams

"Ing. Branislav
Gerzo"
<[Email Removed]>                                          To
[Email Removed]
04/27/2005 02:46                                          cc
AM
Subject
Re: REGEXP

[Email Removed] [D], on Tuesday, April 26, 2005 at 17:12
(-0400) made these points:

D> s/sg//, s/- {1,}(w{1,})//,print +(split)[5,6,7], if (m/f01(d+)/gi )

I think no one replied to you because we don't know what should be
output.


If all you want is the last column, this is a really long way to go about it.

while (<V4>) {
print (split)[7];
print "n";
}

If you want to do output redierection using the shell, you could just
do it on the command line:

perl -lane 'print $F[7]' `samcmd v4 2>&1`

See perldoc perlrun for details. In any case, don't mess with the
builtins (i.e. $,). If you want a "n", print a "n". Also,

s {-*} ()g;

is not a valid substitution statement.

s{-*}()g;

is a valid statement. Notice the lack of spaces. Moving on:

4> open (V4, "samcmd v4 2>&1 |" ) || die "unable to open pipe...
Broken?$!";

Don't do this. the precedence of || is too high. your code attempts
to open a pipe, and if it can't, then it attempts to open "die..." and
starts throwing exceptions. You want:

open (V4, "samcmd v4 2>&1 |" ) or die "unable to open pipe... Broken?$!";

This should be enough to get you started. On the whole, though, I'd
seriously recommend that you pick up a copy of a good intro perl book
(_Learning Perl_ springs to mind), and read the FAQ for this group on
how to identify (bad) examples of Perl 4 code, which it seems you've
been looking at.

HTH,

Jay

Ing. Branislav Gerzo
Jay Savage [JS], on Wednesday, April 27, 2005 at 10:26 (-0400)
thoughtfully wrote the following:

JS> If all you want is the last column, this is a really long way to go about it.
JS> while (<V4>) {
JS> print (split)[7];
JS> print "n";
JS> }

(split)[-1] is better, not ?

--

How do you protect mail on web? I use http://www.2pu.net

[3.141592653589793238642643389504197169.... there's PI in]

Offer Kaye
On 4/27/05, Peter Rabbitson wrote:
QUOTE
also
"or die" is more preferable than "|| die"

Why is that? :)

It's a question of style, it is not better as in "the code will work
better". It is mentioned in "perldoc perlstyle" that it is preferable
to use "or" and "and" instead of "||" and "&&". However, see below for
an example of when || is actually better (IMO).

QUOTE
I was actually going to post a question about ambiguity
syntax later, but here it is anyway. Are the following 4 equivalent?

1)
if ($b) {
$a = $b;
}
else {
$a = $c;
}

2)
$a = $b or $c;

3)
$a = $b || $c;

4)
$a = $b ? $b : $c;


Almost - 1 and 4 are too verbose, and 2 is just plain wrong. You have to write:
$a = ($b or $c);
Since the precedence of "or" is lower than "=", if you write it
without the parens, you're actually writing:
($a = $b) or $c;
which will give you a warning under "use warnings;". So in this case I
feel it is better to write:
$a = $b || $c;
Since in this case the || saves you the trouble of using parens and
the expression means exactly what you think it means. See "perldoc
perlop" for the precedence or Perl operatods.

QUOTE
Also there was an example on the web that completely threw me off. Although
this works:

local ($/);

,I have no idea how it undefs $/. Points to a good reading on the
subject are equally appreciated.


It declares $/ to be local without giving an init value. So now $/ has
the value of "undef". This isn't specific to $/ - local will do the
same to any var:
use Data::Dumper;
our $foo = "foo";
{
local $foo;
print Dumper($foo);
}
print Dumper($foo);

Read:
http://perldoc.perl.org/perlsub.html#Tempo...ues-via-local--

HTH,
--
Offer Kaye

Jay Savage
On 4/27/05, Peter Rabbitson <[Email Removed]> wrote:
QUOTE
also
"or die" is more preferable than "|| die"

Why is that? :) I was actually going to post a question about ambiguity
syntax later, but here it is anyway. Are the following 4 equivalent?


No.

QUOTE
1)
if ($b) {
$a = $b;
}
else {
$a = $c;
}

Pretty self explanatory

QUOTE
2)
$a = $b or $c;

"=" has higher precedence than "or", so it parses as "($a = $b) or
$c". $a will never equal $c. Something else will equal either the
return value of ($a = $b) or $c.

QUOTE
3)
$a = $b || $c;


Same as 1) because || has higher precedence than =. So ti works in
this case. But be careful with things like '$a = $b || $c = $d',
which will parse as '$a = ($b || $c) = $d'.

QUOTE
4)
$a = $b ? $b : $c;


Same as 1)

QUOTE
Also there was an example on the web that completely threw me off. Although
this works:

local ($/);

,I have no idea how it undefs $/. Points to a good reading on the
subject are equally appreciated.


local declares a variable in a particular scope. It saves the current
value of the variable, if any, and reinitializes the variable for the
duration of the curent scope (and only in the current scope). In nine
case out of ten, local will be functionally equivalent to using my,
but It is exclusively for use with globals and package globals; trying
to use it with a normal lexical (I.e. "my") variable will throw an
exception. See perldoc perlvar for details, and why you want to use
local instead of undef on built-ins (i.e., why 'local $/' is better
than 'undef $/' or '$/ = '' ').

HTH,

--jay

JupiterHost.Net
QUOTE
If all you want is the last column, this is a really long way to go about it.

while (<V4>) {
print (split)[7];
print "n";
}

I think that won't work due to some rows formatted like so:

2005/01/20 15:39 17 2% -il-o-b- - - - - sg F01000

unless that was typo?

In that case "7" isn't always the index of the last item in the list
from split.

while (<V4>) {
my @tmp = split;
print "$tmp[ $#tmp ]n";
}

QUOTE
This should be enough to get you started.  On the whole, though, I'd
seriously recommend that you pick up a copy of a good intro perl book
(_Learning Perl_ springs to mind), and read the FAQ for this group on
how to identify (bad) examples of Perl 4 code, which it seems you've
been looking at.

I second that!

Peter Rabbitson
On Wed, Apr 27, 2005 at 09:06:39AM -0500, Peter Rabbitson wrote:
QUOTE
Also there was an example on the web that completely threw me off. Although
this works:

local ($/);


Sorry about this last one, I undrestand the idea of local for globs, I just
thought that

local $/;
and
local ($/);
differ. While reading your answers I realised that it's the same as saying:
my ($a); which is equivalent to my $a; It is pretty weird to have equivalent
operators ( 'or' / '||' ) with different precedence. I guess this is where
TIMTOWTDI bites back really bad... learning something new every day :)

JupiterHost.Net
Ing. Branislav Gerzo wrote:

QUOTE
JupiterHost.Net [JN], on Wednesday, April 27, 2005 at 08:15 (-0500)
thoughtfully wrote the following:


5> my @fa =();
6> my @ha =();


JN> my @fa;
JN> my @ha;
JN> the = () isn't necessary and doesn't keep you from getting uninitialized
JN> value warnings like you think it does, thats only scalars.

I always use for declaring arrays and hashes @foo = (); too. I think
this is proper how to do that. Also it is similar as someone use

No its not, you can if you want but it pointless:

# perl -mstrict -we 'my $u;my @x;print @x;'
# perl -mstrict -we 'my $u;my @x;print $u;'
Use of uninitialized value in print at -e line 1.
#

It was news to me to, there was thread recently with details if you
really want the details.

QUOTE
$|++;, and I use $| = 1;

That two ways to assign 1 to $|, its not similar to the array thing at all.

If you think

@ar = (); is better that @ar; then why not go step further:

my @ar = ();
@ar = () unless @ar;
my $cnt = 0;
for(@ar) { $cnt++; }
die "Array not empty" if @cnt;

why? because its pointless :)

just do

my @ar;

and you're assured an empty list.

Jay Savage
On 4/27/05, Ing. Branislav Gerzo <[Email Removed]> wrote:
QUOTE
Jay Savage [JS], on Wednesday, April 27, 2005 at 10:26 (-0400)
thoughtfully wrote the following:

JS> If all you want is the last column, this is a really long way to go about it.
JS>    while (<V4>) {
JS>        print (split)[7];
JS>        print "n";
JS> }

(split)[-1] is better, not ?


That depends on where you think you're data is going to get corrupted.
If you think something might throw a space in the middle of one of
the the other colums (e.g. "s g F01010"), (split)[-1] will work
better. On the other hand, if you think your input may contain
something like this at you:

6 2005/02/28 22:47 180 100% -il-o-b----- sg F01005 !error:

you need to grab (split)[7]. For general use, I thinkit's basically
a matter of personal preference.

In this case, it probably doesn't really matter: He's done 2>&1 on
the piped open, so there's no way to predict when the buffers will be
flushed or where stderr will be inserted into stdin. some of the
input is going to be munged either way.

Jay

Jay Savage
On 4/27/05, JupiterHost.Net <[Email Removed]> wrote:
QUOTE

If all you want is the last column, this is a really long way to go about it.

while (<V4>) {
print (split)[7];
print "n";
}

I think that won't work due to some rows formatted like so:

2005/01/20 15:39  17  2%  -il-o-b- - - - -  sg F01000

unless that was typo?

In that case "7" isn't always the index of the last item in the list
from split.

while (<V4>) {
my @tmp = split;
print "$tmp[ $#tmp ]n";
}


I was assuming it was a typo/email munge, and that the command he
pipes actually produces consistent output. That may be a faulty
assumption on my part. YOu know what they say about assumptions In
general, though, when parsing log files (which seems to be waht's
going on here) if you're assured reasonably consisten data, it's
better IMNSHO to look for a particular index, because loggers are more
likely to add occasional extranious info or comments at the end of the
line than in the middle.

YMMV,

Jay

JupiterHost.Net
QUOTE
I was assuming it was a typo/email munge, and that the command he
pipes actually produces consistent output.  That may be a faulty
assumption on my part.  YOu know what they say about assumptions In

Its hard to say Derek doesn't give us much to work with :)

QUOTE
general, though, when parsing log files (which seems to be waht's
going on here) if you're assured reasonably consisten data, it's
better IMNSHO to look for a particular index, because loggers are more
likely to add occasional extranious info or comments at the end of the
line than in the middle.

Yeah, I imagine you're correct in your assumption, without actual valid
info its hard to tell :)

Basically if you're gauranteed it index 7 use index 7, otherwise if
you're gauranteed its the last item (and it may be 7 and it may not be(
use the $#array version, if you can't do either redo it all based on
hoew the output is. And if you post to the list about it please send
accurate info.

Luke Bakken
Chris Devers wrote:
QUOTE
On Fri, 1 Jan 1988, amr wrote:

Do we have Perl script that can edit & add to the Windows (XP, 2000)
path.?

Is there a reason that a simple system() command won't work here?

my $status = system( "SET PATH=%PATH%;$new_path" );
die "Couldn't set %PATH%: $!" if $status;

Normally I'm opposed to using system commands when the task can be
done either all in Perl or with a platform- and problem- specific
module, but in this case, the cmd shell's SET command is easy to run
directly, so why not just solve the problem that way?

That will change the environment for the cmd.exe process that is
executed by that one system() call. Changing %ENV will change the
environment for all processes started by the perl script.

Changing the global path requires registry trickery, IIRC.

yes that is true, [5,6,7] need to be typed otherwise all entries are not
accounted for.

Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams





"JupiterHost.Net"
<mlists@jupiterho
st.net> To
[Email Removed]
04/27/2005 11:13 cc
AM
Subject
Re: REGEXP removing - il- - -b-f
and - il- - - - f











QUOTE
If all you want is the last column, this is a really long way to go about
it.

while (<V4>) {
print (split)[7];
print "n";
}

I think that won't work due to some rows formatted like so:

2005/01/20 15:39 17 2% -il-o-b- - - - - sg F01000

unless that was typo?

In that case "7" isn't always the index of the last item in the list
from split.

while (<V4>) {
my @tmp = split;
print "$tmp[ $#tmp ]n";
}

QUOTE
This should be enough to get you started.  On the whole, though, I'd
seriously recommend that you pick up a copy of a good intro perl book
(_Learning Perl_ springs to mind), and read the FAQ for this group on
how to identify (bad) examples of Perl 4 code, which it seems you've
been looking at.

I second that!

--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

perl people.... there was a lot of threads to my question ...thank you! I
will start from the most recent.

I took and understood the advise of my @a = (); changed to my @a;
Yes I do understand the differing precedence between or and | | . I have a
habit using | |. I do also understand that if you use or you should use (
) as opposed to | | you do not have to use ( ). Any comments?

Line 15 is changed to, I forgot ti take out the + . $fa[$i++] =
(split)[-1] if (m/f01(d+)/gi );
But I do not understand what the (split)[-1] is saying? please explain.


I reran the code and it seems to be working now.
thanks again, : )
ciao!


#!/usr/bin/perl
1> use strict;
2> use warnings;
3> $ENV{"PATH"} =
qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);
4> open (V4, "samcmd v4 2>&1 |" ) || die "unable to open pipe... Broken?
$!";
5> my @fa =();
6> my @ha =();
7> my $i =0;
8> foreach (<V4>) {
9> local $, = "n";
10> #print +(split)[6,7], $,;
11> s <sg> ();
12> s {-*} ()g;
13> s {w+} ()i;
14> print +(split)[5,6,7], if (m/f01(d+)/gi )
15> #$fa[$i++] = +(split)[5,6,7] if (m/f01(d+)/gi );
16> #print +(split)[4],$,; #% column
17> }
18> close (V4);
19> print "n";
20> print "Now printing array element 0t", $fa[0], "n";
21> #print "Now printing entire array t", @fa, "n";
22> print "Now printing array count t", $#fa, "n";



Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams





"JupiterHost.Net"
<mlists@jupiterho
st.net> To
[Email Removed]
04/27/2005 12:03 cc
PM
Subject
Re: REGEXP removing - il- - -b-f
and - il- - - - f










QUOTE
I was assuming it was a typo/email munge, and that the command he
pipes actually produces consistent output.  That may be a faulty
assumption on my part.  YOu know what they say about assumptions In

Its hard to say Derek doesn't give us much to work with :)

QUOTE
general, though, when parsing log files (which seems to be waht's
going on here) if you're assured reasonably consisten data, it's
better IMNSHO to look for a particular index, because loggers are more
likely to add occasional extranious info or comments at the end of the
line than in the middle.

Yeah, I imagine you're correct in your assumption, without actual valid
info its hard to tell :)

Basically if you're gauranteed it index 7 use index 7, otherwise if
you're gauranteed its the last item (and it may be 7 and it may not be(
use the $#array version, if you can't do either redo it all based on
hoew the output is. And if you post to the list about it please send
accurate info.

--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

JupiterHost.Net
[Email Removed] wrote:
QUOTE
yes that is true, [5,6,7]  need to be typed otherwise all entries are not

Yes what is true? Please reply inline.

I am confused with your email.

what does (split) [-1] mean?

Finally,

Yes I do understand the differing precedence between or and | | . I have a
habit using | |. I do also understand that if you use or you should use (
) as opposed to | | you do not have to use ( ). Any comments?

Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams





"JupiterHost.Net"
<mlists@jupiterho
st.net> To
[Email Removed]
04/27/2005 02:26 cc
PM
Subject
Re: REGEXP removing - il- - -b-f
and - il- - - - f












[Email Removed] wrote:
QUOTE
yes that is true, [5,6,7]  need to be typed otherwise all entries are not

Yes what is true? Please reply inline.

--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

all f01 records are not printed if one uses print +(split)[7]
rather print +(split)[5,6,7] will print all f01 records.


sorry
derek




"JupiterHost.Net"
<mlists@jupiterho
st.net> To
[Email Removed]
04/27/2005 02:26 cc
PM
Subject
Re: REGEXP removing - il- - -b-f
and - il- - - - f












[Email Removed] wrote:
QUOTE
yes that is true, [5,6,7]  need to be typed otherwise all entries are not

Yes what is true? Please reply inline.

--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

John W. Krahn
[Email Removed] wrote:
QUOTE
yes I agree I was a little ambiguous... I was in a hurry. sorry.  Anyway
here is my updated code. and here is a sample output:
My goal is to get all F01 which I am but I am having issues capturing all
of these values into my array.  When I run the I get the data I want to see
which is just the F01 column, but when I comment out line 14 and uncomment
line 15 and uncomment line 21 I see no data in the array???
In the end I want the F01 column and the % column.

You only want the two columns, that looks simple enough:

while ( <V4> ) {
my ( $percent, $f01 ) = /(d+%).*?(f01d+)/i or next;
print "$percent $f01n";
}


QUOTE
1    2005/01/20 15:39  17  2%  -il-o-b- - - - -  sg F01000
2    2005/01/20 15:53  14  1%  -il-o-b-----  sg F01001
3    2005/01/18 09:53    2  0%  -il-o-b-----  sg F01002
4    2005/02/04 16:41  196 100%  -il-o-b----f  sg F01003
5    2005/02/05 21:13  305 100%  -il-o-b----f  sg F01004
6    2005/02/28 22:47  180 100%  -il-o-b-----  sg F01005
13    2005/02/08 16:07  112 100%  -il-o-b----f  sg F01006
14    2005/02/09 21:56  122 100%  -il-o-b----f  sg F01007
15    2005/02/11 10:51  147 100%  -il-o-b----f  sg F01008
16    2005/02/13 11:35  193 100%  -il-o-b----f  sg F01009
17    2005/02/14 23:46  79 100%  -il-o-b- - - -f  sg F01010


#!/usr/bin/perl
1> use strict;
2> use warnings;
3> $ENV{"PATH"} =
qq(/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log);
4> open (V4, "samcmd v4 2>&1 |" ) || die "unable to open pipe... Broken?

The ampersand is not special in double quoted strings so it doesn't have to be
escaped, or you could just use a single quoted string.

QUOTE
$!";
5> my @fa =();
6> my @ha =();
7> my $i =0;
8>        foreach (<V4>) {

Is there any good reason to slurp the entire file into memory?

QUOTE
9>              local $, = "n";
10>              #print +(split)[6,7], $,;
11>              s <sg> ();
12>              s {-*} ()g;
13>                s {w+} ()i;

As has been pointed out, the whitespace after 's' is an error in current
versions of Perl however some earlier versions would use whitespace as the
delimiter so "s <sg> () ;" would be interpreted as "s/<sg>/()/;".


QUOTE
14>                print +(split)[5,6,7], if (m/f01(d+)/gi )

You are using the match in a boolean context so the /g option makes no sense.


QUOTE
15>              #$fa[$i++] = +(split)[5,6,7] if (m/f01(d+)/gi );

Unlike the print operator, the = operator cannot be used as a function so the
+ operator is not required. Why are you assigning a list to a scalar and why
not just use push() instead of keeping track of the array index?


QUOTE
16>              #print +(split)[4],$,; #% column
17>      }
18> close (V4);

When you open a pipe you should also verify that it closed correctly.

perldoc -f close


QUOTE
19> print "n";
20> print "Now printing array element 0t", $fa[0], "n";
21> #print "Now printing entire array t", @fa, "n";
22> print "Now printing array count t", $#fa, "n";

The value of $#fa is not the number of elements in the array, for that you
want to use the array in scalar context:

print "Now printing array count t", scalar @fa, "n";

Or:

print "Now printing array count t" . @fa . "n";



John
--
use Perl;
program
fulfillment

John W. Krahn
JupiterHost.Net wrote:
QUOTE
[Email Removed] wrote:

yes I agree I was a little ambiguous... I was in a hurry. sorry.  Anyway

Then do it at a later time and do it right, I imagine most all of us are
busy also :)

here is my updated code. and here is a sample output:
My goal is to get all F01 which I am but I am having issues capturing all

Then you're doing a rube goldberg if that is indeed what you're trying
to do:

# untested and prolly even more efficient way but it
# illustrates enhancements to the original code

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;

$ENV{PATH} = '/opt/SUNWsamfs/sbin:/usr/bin:/usr/sbin:/usr/local/log';
open V4, 'samcmd v4 2>&1 |' or die "unable to open pipe: $!";

my @last_col;
for(<V4>) {
chomp;
my @tmp = split /w+/;
push @last_col, $tmp[ $#tmp ];
}

close V4;
print Dumper @last_col;

now @last_col has all of the F01... pieces of data.

You do realize that the characters 'F', '0' and '1' are included in the
character class w which split() is removing? :-)



John
--
use Perl;
program
fulfillment

John W. Krahn
Jay Savage wrote:
QUOTE

4> open (V4, "samcmd v4 2>&1 |" ) || die "unable to open pipe...
Broken?$!";

Don't do this.  the precedence of || is too high.  your code attempts
to open a pipe, and if it can't, then it attempts to open "die..." and
starts throwing exceptions.

No, that is NOT what happens, it will NEVER attempt to open "die..." with or
without the parentheses. The ONLY time it will attempt to open "die..." is if
there are no parentheses and the expression on the left hand side of the ||
operator evaluates to false.

open V4, '0' || die $!;

But even then it will NOT attempt to open "die..." because die() exits the
program!



John
--
use Perl;
program
fulfillment

JupiterHost.Net
QUOTE
You do realize that the characters 'F', '0' and '1' are included in the
character class w which split() is removing?  :-)

yeah I realized that typo too late :), I meant s not w but then plain
old my @tmp = split; is even better :)

John W. Krahn
Ing. Branislav Gerzo wrote:
QUOTE
JupiterHost.Net [JN], on Wednesday, April 27, 2005 at 10:23 (-0500)
thinks about:

JN> No its not, you can if you want but it pointless:

I read it somewhere (it was perl cookbook/learning perl from o'reilly
maybe). Always declare

my @a = ( );

And here is why, if I remember correctly - if you declare
my @a; you should think you declare empty array. After that, in script
you should use @a; which do nothing. So my @a = ( ); is good practise
I think. It is the same for hashes.

"my @a;" creates the lexical variable @a at compile time and since it has just
been created it will be empty. "my @a = ();" creates the variable during
compilation but the assignment (IIRC) has to be done at run time. The end
result is the same but the assignment makes it a tiny bit slower (not enough
to really worry about.)

$ perl -e'
use Benchmark qw(cmpthese);
cmpthese( -10, {
assign => sub { my @a = (); my @b = (); my @c = (); my @d = (); return @a
.. @b . @c . @d },
noassign => sub { my @a; my @b; my @c; my @d; return @a . @b . @c . @d }
} );
'
Rate assign noassign
assign 112694/s -- -2%
noassign 115567/s 3% --


QUOTE
$|++;, and I use $| = 1;
JN> That two ways to assign 1 to $|, its not similar to the array thing at all.

I meant good practise in scripts. What about when somewhere in script
will be $| = -1; pr $|-- ?
$| = 1 will work always.

Any true (numeric) value assigned to $| will set it to 1 and any false
(numeric) value will set it to 0.

$ perl -e'
for ( -876868, -1, 1, 84859, "0", "abcdef", undef ) {
$| = $_;
print "$_ = $_";
print " and $| = ", $|;
print " and ++$| = ", ++$|;
print "n";
}
'
$_ = -876868 and $| = 1 and ++$| = 1
$_ = -1 and $| = 1 and ++$| = 1
$_ = 1 and $| = 1 and ++$| = 1
$_ = 84859 and $| = 1 and ++$| = 1
$_ = 0 and $| = 0 and ++$| = 1
$_ = abcdef and $| = 0 and ++$| = 1
$_ = and $| = 0 and ++$| = 1


--$| and $|-- have the affect of toggling the value of $| between 0 and 1.

$ perl -e'
for ( -876868, -1, 1, 84859, "0", "abcdef", undef ) {
$| = $_;
print "$_ = $_";
print " and $| = $|";
print " and --$| = ", --$|;
print " and --$| = ", --$|;
print "n";
}
'
$_ = -876868 and $| = 1 and --$| = 0 and --$| = 1
$_ = -1 and $| = 1 and --$| = 0 and --$| = 1
$_ = 1 and $| = 1 and --$| = 0 and --$| = 1
$_ = 84859 and $| = 1 and --$| = 0 and --$| = 1
$_ = 0 and $| = 0 and --$| = 1 and --$| = 0
$_ = abcdef and $| = 0 and --$| = 1 and --$| = 0
$_ = and $| = 0 and --$| = 1 and --$| = 0



John
--
use Perl;
program
fulfillment

I will answer and ask all questions in one email! k.

QUOTE
$!";
5> my @fa =();
6> my @ha =();
7> my $i =0;
8>        foreach (<V4>) {

Is there any good reason to slurp the entire file into memory?

What would you suggest? I want to read the entire file via a filehandle.
I have plenty of system memory, therefore why not?



QUOTE
You are using the match in a boolean context so the /g option makes no
sense.
15>              #$fa[$i++] = +(split)[5,6,7] if (m/f01(d+)/gi );

If I do not use the /g modifier then it will not slurp the entire file or
all instances of F01, I tried it without /g and it did not work.


QUOTE
The value of $#fa is not the number of elements in the array, for that you
want to use the array in scalar context:

print "Now printing array count t", scalar @fa, "n";

Or:

print "Now printing array count t" . @fa . "n";


In my Learning Perl 2nd edition and Programming Perl 3rd edition, no where
does it say use
scaler @fa to get the element count, rather it says use $#fa. Is there
something I am unaware of or was $# deprecated recently?

Why is scaler @fa better/more correct than $#fa?

I have been told that my @a = ( ); is more correct than my @a, but now I am
confused b/c others are telling me otherwise??? Personally, I like my @a =
( );
because it lets me know explicitly that this array is now initialized to 0
elements, plus its faster at compile time...KUDOS to John Kran!

From Jupiter :

QUOTE
You do realize that the characters 'F', '0' and '1' are included in the
character class w which split() is removing?  :-)

yeah I realized that typo too late :), I meant s not w but then plain
old my @tmp = split; is even better :)

But I thought spit by default separates on whitespace? And F, 0 and 1 are
indeed apart of char classes w and d?

Finally no one has answered my question what does (split)[-1] mean?


thank you!
derek


Derek B. Smith
OhioHealth IT
UNIX / TSM / EDM Teams





"John W. Krahn"
<[Email Removed]
QUOTE
To
Perl Beginners <[Email Removed]

04/27/2005 05:24 cc
PM
Subject
Re: REGEXP removing - il- - -b-f
and - il- - - - f










[Email Removed] wrote:
QUOTE
yes I agree I was a little ambiguous... I was in a hurry. sorry.  Anyway
here is my updated code. and here is a sample output:
My goal is to get all F01 which I am but I am having issues capturing all
of these values into my array.  When I run the I get the data I want to
see
which is just the F01 column, but when I comment out line 14 and
uncomment
line 15 and uncomment line 21 I see no data in the array???
In the end I want the F01 column and the % column.

You only want the two columns, that looks simple enough:

while ( <V4> ) {
my ( $percent, $f01 ) = /(d+%).*?(f01d+)/i or next;
print "$percent $f01n";
}


QUOTE
1    2005/01/20 15:39  17  2%  -il-o-b- - - - -  sg F01000
2    2005/01/20 15:53  14  1%  -il-o-b-----  sg F01001
3    2005/01/18 09:53    2  0%  -il-o-b-----  sg F01002
4    2005/02/04 16:41  196 100%  -il-o-b----f  sg F01003
5    2005/02/05 21:13  305 100%  -il-o-b----f  sg F01004
6    2005/02/28 22:47  180 100%  -il-o-b-----  sg F01005
13    2005/02/08 16:07  112 100%  -il-o-b----f  sg F01006
14    2005/02/09 21:56  122 100%  -il-o-b----f  sg F01007
15    2005/02/11 10:51  147 100%  -il-o-b----f  sg F01008
16    2005/02/13 11:35  193 100%  -il-o-b----f  sg F01009
17    2005/02/14 23:46  79 100%  -il-o-b- - - -f  sg F01010


<SNIP>
John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [Email Removed]
For additional commands, e-mail: [Email Removed]
<http://learn.perl.org/> <http://learn.perl.org/first-response>

Laxmi Goudappa Patil
Hi All,

Thanks for your response.

sorry (for bad subject).. i'll take care of that from next time.

The operating system im working on is SCO Unix, and the device(port) is /dev/tty1a

The code looks something like this.

writeport($request); #writing connect request
my(@READYFD) = $FD_SET->can_read(0);
foreach $fd (@READYFD){
print "Inside foreach : $fd";

my $msg = readport($FD);#expecting "PAD:" characters
if($msg eq $PAD){} #ignoring those characters
$msg = readport($FD); #expecting "CC" call connect
print "message is $msg ";
print "call connect :$CallConnect";
sleep(0.5);
if(($fd == $FD) && ($msg eq $CallConnect)){
print "inside call connect";
$msg = readport($FD); #expecting XON before writing mesg
if($msg eq $XON){
writemesg($port);
}

like this goes on..
Actually i always a expect some message in response to the request i sent.finally i write actual message to the port.
Above, after first read call, if the other end is not writing anything means, should return an empty string is it?? but it's returning the same characters, the first read call returns.. so should i have to flush the file descriptor before second read?? if so how to use it ?? i tried to make the situation understandable..Hope this is what you expected , to help in this regard.


Thanks.

On Wed, 27 Apr 2005 Chris Devers wrote :
QUOTE
Please use a descriptive subject line. Half the spam I get has "Hello"
for a subject, so your message nearly got flagged as spam & deleted.

On Wed, 27 Apr 2005, laxmi goudappa patil wrote:

Doing serial programming in perl. when i read a port, if there is data
on the port it reads successfully. if there is no data at that moment
im recieving the same data which hasn't written to the port..

i have two parts of the program.. one is writing to the port, and the
other is reading from the port..can you please help, understanding why
it's happening like this?? and what can i do to overcome this
problem??

What operating system are you working on? Windows? Linux? Other?

What does your code look like? It's impossible to make suggestions
without being able to read the section of code that isn't behaving.

More information is needed in order to help you.



--
Chris Devers


John W. Krahn
[Email Removed] wrote:
QUOTE
I will answer and ask all questions in one email!  k.

8>        foreach (<V4>) {

Is there any good reason to slurp the entire file into memory?

What would you suggest?  I want to read the entire file via a filehandle.
I have plenty of system memory, therefore why not?

Why not indeed? There is no reason not to if that is what you really want to do.


QUOTE
You are using the match in a boolean context so the /g option makes no
sense.

15>              #$fa[$i++] = +(split)[5,6,7] if (m/f01(d+)/gi );

If I do not use the /g modifier then it will not slurp the entire file or
all instances of F01,  I tried it without /g and it did not work.

According to the data you posted, and that you had not modified the value in
$/, then $_ would only contain one instance of 'F01'. And even if it
contained all instances of 'F01' the match operator is used in boolean
(scalar) context and you are not assigning the list it produces nor are you
using the results from the capturing parentheses. And using or not using the
/g option has no effect on whether or not the file will be slurped.

When you say "it did not work" what exactly do you mean?


QUOTE
The value of $#fa is not the number of elements in the array, for that you
want to use the array in scalar context:

print "Now printing array count t", scalar @fa, "n";

Or:

print "Now printing array count t" . @fa . "n";

In my Learning Perl 2nd edition and Programming Perl 3rd edition, no where
does it say use
scaler @fa to get the element count, rather it says use $#fa.  Is there
something I am unaware of or was $# deprecated recently?

Why is scaler @fa better/more correct than $#fa?

Because the array in scalar context will always be equal to the number of
elements in the array.

$ perl -le'
my @a = qw/ a b c d e f /; # six elements

print qq(Number of elements in "@a" = ), scalar @a;
print "Index of last element = $#a, contents = $a[$#a]";

{ local $[ = 5;

print qq(Number of elements in "@a" = ), scalar @a;
print "Index of last element = $#a, contents = $a[$#a]";
}
'
Number of elements in "a b c d e f" = 6
Index of last element = 5, contents = f
Number of elements in "a b c d e f" = 6
Index of last element = 10, contents = f


QUOTE
I have been told that my @a = ( ); is more correct than my @a, but now I am
confused b/c others are telling me otherwise???

Neither one is *more* correct, they both result in the same thing, it's just
that "my @a = ();" is assigning emptiness to something that is already empty.
It's sorta like saying:

( $a = 9 ) = 9;

The second assignment doesn't do anything more then the first assignment has
already done.


QUOTE
Personally, I like my @a = ( );
because it lets me know explicitly that this array is now initialized to 0
elements, plus its faster at compile time

I think that you misinterpreted the benchmark results that I posted.


QUOTE
From Jupiter :

my @tmp = split /w+/;

You do realize that the characters 'F', '0' and '1' are included in the
character class w which split() is removing?  :-)

yeah I realized that typo too late :), I meant s not w but then plain
old my @tmp = split; is even better :)

But I thought spit by default separates on whitespace?

Yes it does. (spit??)


QUOTE
And F, 0 and 1 are
indeed apart of char classes w  and d?

w includes 'F', '0' and '1' while d includes '0' and '1'.


QUOTE
Finally no one has answered my question what does (split)[-1] mean?

perldoc perldata

[snip]

A list value may also be subscripted like a normal array. You must put
the list in parentheses to avoid ambiguity. For example:

# Stat returns list value.
$time = (stat($file))[8];

# SYNTAX ERROR HERE.
$time = stat($file)[8]; # OOPS, FORGOT PARENTHESES

# Find a hex digit.
$hexdigit = ('a','b','c','d','e','f')[$digit-10];

# A "reverse comma operator".
return (pop(@foo),pop(@foo))[0];


See also the "Slices" section of perldata.



John
--
use Perl;
program
fulfillment

Ing. Branislav Gerzo
[Email Removed] [D], on Wednesday, April 27, 2005 at 12:55
(-0400) wrote the following:

D> But I do not understand what the (split)[-1] is saying? please explain.

it gets last splitted value from the list.
(split)[0] #get the first splitted value
(split)[0,1] #get first and second splitted value (assign it to @array)
(split)[-1] #get the first value from the end of list


--

How do you protect mail on web? I use http://www.2pu.net

[Punny Book: Fastest Gun In The West - Everett DeReady]

JupiterHost.Net
QUOTE
(really beginners) could think "@a" will empty array, which is not
true.

yes it is true, they are both empty lists:

perl -mstrict -MData::Dumper -we 'my @x;my @y = ();print Dumper @x, @y;'

Show me any difference in @x and @y :)

Its about when and how its handled and the time it takes to handle it.

See John's excellent explanation earlier for more details.

JupiterHost.Net
Ing. Branislav Gerzo wrote:

QUOTE
JupiterHost.Net [JN], on Thursday, April 28, 2005 at 09:11 (-0500)
contributed this to our collective wisdom:


(really beginners) could think "@a" will empty array, which is not
true.


JN> yes it is true, they are both empty lists:

@a will not empty array, here it is:

I never said it will empty a list, I'm talking about declaring it
intially (IE my @a; vs. my @a = ();) when I brought it up.

# perl -mstrict -MData::Dumper -we 'my @x;my @y = ();print Dumper @x, @y;'
$VAR1 = [];
$VAR2 = [];
#

See? No differnece in the array, both *create* an empty list, just
my @a; is slightly more efficient.

QUOTE
my @a = q{foo bar};
@a;
print @a;

against

my @a = q{foo bar};
@a = ( );
print @a;

I hope you see the difference :)

Yes the difference now you're not talking about declaring the array
initially which is what this part of this lame thread is all about.

Apples and Oranges my friend :)

PS - I think you mean qw() not q() as you're only assigning one string
of "foo bar" to the array...

# perl -mstrict -MData::Dumper -we 'my @x = q{foo bar};print Dumper @x;'
$VAR1 = [
'foo bar'
];
#

vs.

# perl -mstrict -MData::Dumper -we 'my @x = qw(foo bar);print Dumper @x;'
$VAR1 = [
'foo',
'bar'
];
#

Jay Savage
On 4/27/05, John W. Krahn <[Email Removed]> wrote:
QUOTE
Jay Savage wrote:

4> open (V4, "samcmd v4 2>&1 |" ) || die "unable to open pipe...
Broken?$!";

Don't do this.  the precedence of || is too high.  your code attempts
to open a pipe, and if it can't, then it attempts to open "die..." and
starts throwing exceptions.

No, that is NOT what happens, it will NEVER attempt to open "die..." with or
without the parentheses.  The ONLY time it will attempt to open "die..." is if
there are no parentheses and the expression on the left hand side of the ||
operator evaluates to false.

open V4, '0' || die $!;

But even then it will NOT attempt to open "die..." because die() exits the
program!


John

Is stand corrected. There is no exception; I guess any time I've run
into it, I've relied on whatever was opened, and died anyway. But I
don't know what else to call opens behavior, except attempting to open
die. Except in the case of parenthesis, as you noted, the behavior of
open || die sure looks like this to me: open (X, badfile || die).
The only difference between the two expressions below is the
precedence of the operator.

jsavage@ariadne:~> perl -e 'open FH, "< BAdFiLe" || die "$!"'
jsavage@ariadne:~> perl -e 'open FH, "< BAdFiLe" or die "$!"'
No such file or directory at -e line 1.

Open may not technically be trying to open an expression and failing,
I don't know. To be honest, I've never taken apart the source to see.
But the appearance is certainly that that's what happens, and the
result is so similar as to not matter. Especially consider the
following:

perl -e 'open FH, "< BAdFiLe" || die or die "$!"; print "didnt dien" '
No such file or directory at -e line 1.

Where did the first die go if || didn't attempt to pass it to open?

if the reason for the failed open were the attempt to open "BAdFiLe",
the first die would execute and the program would exit bore it got to
the second. But clearly that's not what's happening. The first die
is getting slurped up by ||, which is presumably trying to pass it on
to open. When that fails, the second die executes, exiting with $!.
at least that's what it looks like to me.

So what's really happeneing here?

--jay


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.