[16315] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3727 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jul 18 10:58:12 2000

Date: Tue, 18 Jul 2000 07:58:02 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <963932281-v9-i3727@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Tue, 18 Jul 2000     Volume: 9 Number: 3727

Today's topics:
        Read a file into a hash ? (Fidelio)
    Re: Read a file into a hash ? (Ulrich Ackermann)
    Re: Read a file into a hash ? (Jakob Schmidt)
    Re: Read a file into a hash ? (Ramanujam Parthasarathi)
    Re: Read a file into a hash ? (Ramanujam Parthasarathi)
    Re: Read a file into a hash ? (Ramanujam Parthasarathi)
    Re: Read a file into a hash ? (Larry Rosler)
    Re: Read a file into a hash ? (Jakob Schmidt)
    Re: Read a file into a hash ? (Larry Rosler)
    Re: Read a file into a hash ? (Logan Shaw)
    Re: Read a file into a hash ? (Logan Shaw)
    Re: Reading lines of a file within a loop? (Ulrich Ackermann)
    Re: Reading lines of a file within a loop? (Bernard El-Hagin)
    Re: Reading lines of a file within a loop? (Tad McClellan)
        Reading/modifying file - perl script help (John Casey)
        Redirect External Program's output on Windows ? ()
        Redirect External Program's output on Windows ? ()
        Redirect External Program's output on Windows ? ()
        Redirect External Program's output on Windows ? ()
    Re: Redirect External Program's output on Windows ? ()
    Re: Redirect External Program's output on Windows ? ()
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 17 Jul 2000 09:50:05 GMT
From: rjn@pobox.com.bbs@openbazaar.net (Fidelio)
Subject: Read a file into a hash ?
Message-Id: <3bR7fT$WPY@openbazaar.net>

Hi,

I want to read a file into a hash. I now have code like:
    open(PASSWD, '/etc/passwd');
    while (<PASSWD>) {
        ($login, $passwd, $uid, $gid,
         $gcos, $home, $shell) = split(/:/);
        # do something
    }

Now everytime I want to access a user's home dir for example I
have to read the file.
Suppose I read the file into an array like:

  open(PASSWD, '/etc/passwd');
  @users = <PASSWD>;
  close (PASSWD);
  foreach $lines(@users) {
    chop($lines);
    @line = split(/\:/,$lines);
    # put users into %users ??
  }

Then I can access all the fields with @user{'root'} and eg. the home
dir with $user{'root'}[5]  right ?
I a lost how to do this. @user{$line[0]} = @line  just doesn't work.



R

--
      -=*=-     | Don't be a coward - use your real email address
  Rob J. Nauta  | Fidelio's beurspagina: http://www.xs4all.nl/~rob/beurs.html
 rjn@pobox.com  | Personal URL: http://www.xs4all.nl/~rob/       ICQ: 5369291
      -=*=-     | The Adsenger message Forum: http://www.delphi.com/adsenger


------------------------------

Date: 17 Jul 2000 11:20:01 GMT
From: uackermann@orga.com.bbs@openbazaar.net (Ulrich Ackermann)
Subject: Re: Read a file into a hash ?
Message-Id: <3bRAA1$W4H@openbazaar.net>

Fidelio wrote:
> Suppose I read the file into an array like:
>
>   open(PASSWD, '/etc/passwd');
>   @users = <PASSWD>;
>   close (PASSWD);
>   foreach $lines(@users) {
>     chop($lines);
>     @line = split(/\:/,$lines);
>     # put users into %users ??
>   }
>
> Then I can access all the fields with @user{'root'} and eg. the home
> dir with $user{'root'}[5]  right ?
> I a lost how to do this. @user{$line[0]} = @line  just doesn't work.
                          ^^^               ^^^
A hash can only hold strings as values. If you want to hold an array,
you've got to store the reference of that array. If you had used the
"-w" switch in your shebang-line you should have received a
warning-message with your code. BTW: You should do this in every script
you are writing!

So, if you want to store the home dir of root in a hash, after having
read your line into @line, this probably might work:

$user{$line[0]}{dir} = $line[5].

I'm relativly new to Perl, so use my hints with care ;-)

HTH, Ulrich
--
Ulrich Ackermann
ORGA Kartensysteme GmbH (SY-PEAT-STA)
Tel.:+49.5254.991-925
mailto:uackermann@orga.com


------------------------------

Date: 17 Jul 2000 11:40:00 GMT
From: sumus@aut.dk.bbs@openbazaar.net (Jakob Schmidt)
Subject: Re: Read a file into a hash ?
Message-Id: <3bRAZ0$VS7@openbazaar.net>

rjn@pobox.com (Fidelio) writes:

There must be a million ways but here's one starting with your own code:

>   open(PASSWD, '/etc/passwd');

Remember check:

open(PASSWD, '/etc/passwd') or die "Heck! It said: $!";

>   @users = <PASSWD>;
>   close (PASSWD);

This is OK but there's really no reason to slurp the entire file. Your
approach from the first code snippet was better:

while ( <PASSWD> ) {
 ....

In stead of $lines just use $_ in the loop body and the code does the same
thing.

>   foreach $lines(@users) {
(while...)
>     chop($lines);

just chop;

>     @line = split(/\:/,$lines);

just @line = split ':';

>     # put users into %users ??

$users{ shift( @line ) } = [ @line ];

>   }

Here's the cleanup:

use strict;
my %users;

open(PASSWD, '/etc/passwd') or die "Heck! It said: $!";
while ( <PASSWD> ) {
    chop;
    my @line = split ':';
    $users{ shift( @line ) } = [ @line ];
}

# %users is ready for use

--
Jakob


------------------------------

Date: 17 Jul 2000 12:40:03 GMT
From: partha@mihy.mot.com.bbs@openbazaar.net (Ramanujam Parthasarathi)
Subject: Re: Read a file into a hash ?
Message-Id: <3bRCE3$VuE@openbazaar.net>

Fidelio wrote:

>   open(PASSWD, '/etc/passwd');
>   @users = <PASSWD>;
>   close (PASSWD);
>   foreach $lines(@users) {
>     chop($lines);
>     @line = split(/\:/,$lines);
>     # put users into %users ??
>   }

# Try doing this in the foreach loop ...
# assuming that user-name is the first entry in the 'passwd' file
$user = shift(@users);
$user_hash{$user} = [ @users ];
-------
# Now, you can access the details of each user as:
foreach $users (sort keys %users_hash ) {
   print("$users => ", join(":", @$users_hash), "\n");
}
-------
# For a purticular entry in the list, you can use:
print("User-id of $user => $$users_hash[2]\n");
-------

HTH
-Partha


------------------------------

Date: 17 Jul 2000 12:40:03 GMT
From: partha@mihy.mot.com.bbs@openbazaar.net (Ramanujam Parthasarathi)
Subject: Re: Read a file into a hash ?
Message-Id: <3bRCE5$W08@openbazaar.net>

Fidelio wrote:

>   open(PASSWD, '/etc/passwd');
>   @users = <PASSWD>;
>   close (PASSWD);
>   foreach $lines(@users) {
>     chop($lines);
>     @line = split(/\:/,$lines);
>     # put users into %users ??
>   }

# Try doing this in the foreach loop ...
# assuming that user-name is the first entry in the 'passwd' file
$user = shift(@users);
$user_hash{$user} = [ @users ];
-------
# Now, you can access the details of each user as:
foreach $users (sort keys %users_hash ) {
   print("$users => ", join(":", @$users_hash), "\n");
}
-------
# For a purticular entry in the list, you can use:
print("User-id of $user => $$users_hash[2]\n");
-------

HTH
-Partha


------------------------------

Date: 17 Jul 2000 12:40:03 GMT
From: partha@mihy.mot.com.bbs@openbazaar.net (Ramanujam Parthasarathi)
Subject: Re: Read a file into a hash ?
Message-Id: <3bRCE4$Vy9@openbazaar.net>

Fidelio wrote:

>   open(PASSWD, '/etc/passwd');
>   @users = <PASSWD>;
>   close (PASSWD);
>   foreach $lines(@users) {
>     chop($lines);
>     @line = split(/\:/,$lines);
>     # put users into %users ??
>   }

# Try doing this in the foreach loop ...
# assuming that user-name is the first entry in the 'passwd' file
$user = shift(@users);
$user_hash{$user} = [ @users ];
-------
# Now, you can access the details of each user as:
foreach $users (sort keys %users_hash ) {
   print("$users => ", join(":", @$users_hash), "\n");
}
-------
# For a purticular entry in the list, you can use:
print("User-id of $user => $$users_hash[2]\n");
-------

HTH
-Partha


------------------------------

Date: 17 Jul 2000 19:50:04 GMT
From: lr@hpl.hp.com.bbs@openbazaar.net (Larry Rosler)
Subject: Re: Read a file into a hash ?
Message-Id: <3bRNNU$X1y@openbazaar.net>

In article <snt96l08.fsf@macforce.sumus.dk> on 17 Jul 2000 13:42:31
+0200, Jakob Schmidt <sumus@aut.dk> says...

 ...

> >   foreach $lines(@users) {
> (while...)
> >     chop($lines);
>
> just chop;

Using Perl 5 (and aren't we all?): chomp;

> >     @line = split(/\:/,$lines);
>
> just @line = split ':';
>
> >     # put users into %users ??
>
> $users{ shift( @line ) } = [ @line ];

Because this statement both copies @line and shifts an element from it,
it shouldn't be done the way you have it, because of ambiguity in the
order in which the operations are performed.

  my $login = shift @lines;
  $users{$login} = [ @line ];

or

  $users{$line[0]} = [ @line[1 .. $#line] ];

One might Benchmark to see which way is faster, if one cared.

> >   }

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


------------------------------

Date: 17 Jul 2000 21:40:02 GMT
From: sumus@aut.dk.bbs@openbazaar.net (Jakob Schmidt)
Subject: Re: Read a file into a hash ?
Message-Id: <3bRQH4$WG0@openbazaar.net>

Larry Rosler <lr@hpl.hp.com> writes:

> In article <snt96l08.fsf@macforce.sumus.dk> on 17 Jul 2000 13:42:31
> +0200, Jakob Schmidt <sumus@aut.dk> says...
>
> > just chop;
>
> Using Perl 5 (and aren't we all?): chomp;

Certainly. But I think I remember that chop is faster (as it would seem
obvious) so it may still be defended in a cas such as this where the input is
very disciplined. You may argue of course that the solutions posted in this
forum serve as general examples as well and ought to be fool proof.

> > $users{ shift( @line ) } = [ @line ];
>
> Because this statement both copies @line and shifts an element from it,
> it shouldn't be done the way you have it, because of ambiguity in the
> order in which the operations are performed.

I missed that one thinking naïvely that perl would evaluate shift( @line )
first. Apparantly that is not the case, sorry and thanks for noticing. So
I learned something too.

Oh and by the way - I think that I ought to have included an explicit close
too:

close PASSWD;

Far as I can see there's no consensus for checking the success of close()
(on filehandles). Maybe because finding that it doesn't is just so embarrassing
that you don't even want to know?

--
Jakob


------------------------------

Date: 17 Jul 2000 22:30:04 GMT
From: lr@hpl.hp.com.bbs@openbazaar.net (Larry Rosler)
Subject: Re: Read a file into a hash ?
Message-Id: <3bRRVT$TXa@openbazaar.net>

In article <k8ektozh.fsf@macforce.sumus.dk> on 17 Jul 2000 23:40:18
+0200, Jakob Schmidt <sumus@aut.dk> says...

 ...

> Oh and by the way - I think that I ought to have included an explicit close
> too:
>
> close PASSWD;
>
> Far as I can see there's no consensus for checking the success of close()
> (on filehandles). Maybe because finding that it doesn't is just so embarrassing
> that you don't even want to know?

Closing a file opened for reading is pointless, unless another process
or later in this process intends to access it.

Closing a file opened for writing might reveal failure to write the
file, for lack of disk space or other size limits.  Intervening print
statements are very seldom checked, so this is the only way.

Closing pipes must be checked, as you seem to know.

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


------------------------------

Date: 17 Jul 2000 23:10:03 GMT
From: logan@cs.utexas.edu.bbs@openbazaar.net (Logan Shaw)
Subject: Re: Read a file into a hash ?
Message-Id: <3bRSXU$UqT@openbazaar.net>

In article <MPG.13dd297feb3984b098abda@nntp.hpl.hp.com>,
Larry Rosler  <lr@hpl.hp.com> wrote:
>Closing a file opened for reading is pointless, unless another process
>or later in this process intends to access it.

Or unless the system administrator wants to unmount the disk.

  - Logan, a former system administrator


------------------------------

Date: 17 Jul 2000 23:20:03 GMT
From: logan@cs.utexas.edu.bbs@openbazaar.net (Logan Shaw)
Subject: Re: Read a file into a hash ?
Message-Id: <3bRSk8$SqT@openbazaar.net>

In article <8kulco$8ak$1@xs4.xs4all.nl>, Fidelio <rjn@pobox.com> wrote:
>I want to read a file into a hash. I now have code like:
>    open(PASSWD, '/etc/passwd');
>    while (<PASSWD>) {
>        ($login, $passwd, $uid, $gid,
>         $gcos, $home, $shell) = split(/:/);
>        # do something
>    }

If you're just using this as an example, then others' suggestions are
great.

If you really want to see what's in the password file (or database --
NIS, NIS+, LDAP, etc.), then I suggest a completely different approach:

	#! /usr/local/bin/perl

	use User::pwent;

	while ($user = getpwent)
		{
		printf ("%s's home directory is %s\n", $user->name, $user->dir);
		}

Or something like that.

  - Logan


------------------------------

Date: 12 Jul 2000 12:30:04 GMT
From: uackermann@orga.com.bbs@openbazaar.net (Ulrich Ackermann)
Subject: Re: Reading lines of a file within a loop?
Message-Id: <3bNGPS$Wjx@openbazaar.net>

John Casey wrote:
>
> Hi,
>
> I need to write a script to scan a  XML file for the lines containing the search
> pattern "GEEQualify" .   The line would be typically like this, (with the values
> of the variables being different for different lines):
>
> <GEEQualify  Length="9.979" DCode="A6" Speed="970"/>
>
>   Then, when this line is found, it has the to check the value of
> the "Speed" variable (eg: Speed="970" here) in the line. If it is above  500,
> the script has to proceed to do the following (otherwise it has to continue
> the search for the remaining of the file ); Scan the next two
> lines which would be as follows:
> ******
> <NUM>86756</NUM>
> <Info Available="Y"  Name="Jackie" GEEAvailable="Fri Jun 21, 2000 00:00:00 EDT"
> LateC="128"/>
> ******
>    It has to take the numeric value (86756 here) between the <NUM> tags
> and the alphabetic value of the 'Available' variable (Y here)
> and print them out in a new file  with the format
>    86756#Y#
>
> ---
> The outline of the script i am trying to work on is
> While (<FILEID>)
> {
     my $NUM; # better written in lc
     my $AVAILABLE; # better written in lc

>    chomp;
>    if /GEEQualify/
>    {
>       $line = $_;
>         ????? (How do i pick the "speed=xyz" variable from the $line? ) ??
	$line =~ /Speed=\"(\d+)\"/;
	my $speed = $1;

>      if ($speed >= 500 )
>      {
>         ???(how do i) read the next two lines within the loop ???
>         ?? PatternMatch the two lines to get the required NUM and Available
>                values??
	  <FILEID> =~ /<NUM>(\d+)</;
	  $NUM = $1;
	  <FILEID> =~ /Available\"(\w)\"/;
	  $AVAILABLE = $1;
>
>              print OUTPUTFILE, " $NUM#$AVAILABLE# \n"
>      }
> }
>
>
> Please suggest on how to read two lines within a loop and
> the regular expression for pattern matching the two lines to get the values.
>
> thanks
> John

Maybe this works?! I didn't test it. And I'm a Perl-Novice, too!

Ulrich
--
Ulrich Ackermann
ORGA Kartensysteme GmbH (SY-PEAT-STA)
Tel.:+49.5254.991-925
mailto:uackermann@orga.com


------------------------------

Date: 12 Jul 2000 13:30:03 GMT
From: bernard.el-hagin@lido-tech.net.bbs@openbazaar.net (Bernard El-Hagin)
Subject: Re: Reading lines of a file within a loop?
Message-Id: <3bNI4R$ZGI@openbazaar.net>

Ulrich Ackermann <uackermann@orga.com> wrote:
>John Casey wrote:
>>
>> Hi,
>>
>> I need to write a script to scan a  XML file for the lines containing the search
>> pattern "GEEQualify" .   The line would be typically like this, (with the values
>> of the variables being different for different lines):
>>
>> <GEEQualify  Length="9.979" DCode="A6" Speed="970"/>
>>
>>   Then, when this line is found, it has the to check the value of
>> the "Speed" variable (eg: Speed="970" here) in the line. If it is above  500,
>> the script has to proceed to do the following (otherwise it has to continue
>> the search for the remaining of the file ); Scan the next two
>> lines which would be as follows:
>> ******
>> <NUM>86756</NUM>
>> <Info Available="Y"  Name="Jackie" GEEAvailable="Fri Jun 21, 2000 00:00:00 EDT"
>> LateC="128"/>
>> ******
>>    It has to take the numeric value (86756 here) between the <NUM> tags
>> and the alphabetic value of the 'Available' variable (Y here)
>> and print them out in a new file  with the format
>>    86756#Y#
>>
>> ---
>> The outline of the script i am trying to work on is
>> While (<FILEID>)

while shouldn't be capitalised.

>> {
>     my $NUM; # better written in lc
>     my $AVAILABLE; # better written in lc
>
>>    chomp;
>>    if /GEEQualify/

This won't compile, but this...

if (/GEEQualify/)

 ...will.
	
>>    {
>>       $line = $_;
>>         ????? (How do i pick the "speed=xyz" variable from the $line? ) ??
>	$line =~ /Speed=\"(\d+)\"/;
>	my $speed = $1;

There's no need for the $line variable. There's also no need to escape
the "'s in the regex. And this won't compile anyway if you have "use
strict" on (as you should) because you're later using $speed outside
of its scope. If you want $speed to be global you have to declare it
globally.

    m/Speed="(\d+)"/;
    $speed = $1; #provided $speed has been declared globally

>>      if ($speed >= 500 )
            ^^^^^^
This is where "use strict" will cause the script to die unless you make
$speed global.

>>      {
>>         ???(how do i) read the next two lines within the loop ???
>>         ?? PatternMatch the two lines to get the required NUM and Available
>>                values??
>	  <FILEID> =~ /<NUM>(\d+)</;
>	  $NUM = $1;
>	  <FILEID> =~ /Available\"(\w)\"/;
>     $AVAILABLE = $1;

This should read:

	m#<NUM>(\d+)</NUM>#;
    $NUM = $1;
	m/Available="([^"]+)"/; #unless the value of available can only be Y or N
	                        #in which case m/Available="(Y|N)"/ will do
	$AVAILABLE = $1;

But there's still the fact that if both of these match once then you're ok, but
if, for example, you get a match for both and later in the file you only
get a match for, say, $AVAILABLE, you'll get ouput with the new $AVAILABLE
and the *old* $NUM.

And, as before, you don't need to escape the "'s.

>>      print OUTPUTFILE, " $NUM#$AVAILABLE# \n"

"No comma allowed after filehandle". Drat.

>>      }
>> }
>>
>>
>> Please suggest on how to read two lines within a loop and
>> the regular expression for pattern matching the two lines to get the values.
>>
>> thanks
>> John

Bernard
--
perl -e'@x=(3,2,4,1,3,2,1,3,1,3,2,3,3,2,3,0,0,1,2,1,1,1,4,1,2,1,1,2,2,1,
2,1,2,1,2,1,2,1,1,1,2,1,0,0,3,2,3,2,3,2,1,1,1,1,1,2,4,2,3,2,1,2,1,0,0,1,
2,1,1,1,4,1,2,1,1,1,2,2,1,1,4,1,1,1,2,1,1,1,2,1,0,0,3,2,4,1,1,2,1,1,1,3,
1,1,1,4,1,1,1,2,1,1,3,0,0);sub x{print q x$xx$_;print q x x x shift@x};#
while(defined($_=shift @x)){s o0o\no;$_!=0?x:print}' #Symmetry yrtemmyS#


------------------------------

Date: 12 Jul 2000 14:30:04 GMT
From: tadmc@metronet.com.bbs@openbazaar.net (Tad McClellan)
Subject: Re: Reading lines of a file within a loop?
Message-Id: <3bNJVW$V41@openbazaar.net>

[ Please limit your line lengths to the customary 70-72 characters. ]


On 11 Jul 2000 21:35:51 -0700, John Casey <John_member@newsguy.com> wrote:

>
>I need to write a script to scan a  XML file for the lines containing the search


You should be very sure that you really want to do this using
a pattern match.

Parsing XML with a real XML parser is a much safer way to go about it...


>pattern "GEEQualify" .   The line would be typically like this, (with the values
>of the variables being different for different lines):
>
><GEEQualify  Length="9.979" DCode="A6" Speed="970"/>

>Scan the next two
>lines which would be as follows:


XML lets you put newlines in "funny" places.

There is no guarantee that the elements will be on "lines"
(unless you control all of the XML data yourself).

This is a legal XML tag:

<GEEQualify
Length="9.979"
DCode="A6"
Speed="970"
/>


If you pattern match, that will be a problem.

If you are using an XML parsing module, that will not be a problem...



>Please suggest on how to read two lines within a loop and


while (<FILE>) {
   if (  /something/ ) {   # read the next two lines
      my $line1 = <FILE>;
      my $line2 = <FILE>;
   }
}


>the regular expression for pattern matching the two lines to get the values.


------------
# this is very fragile code!
if ( /<GEEQualify\s+
       Length\s*=\s*"([^"]*)"\s+
       DCode\s*=\s*"([^"]*)"\s+
       Speed\s*=\s*"([^"]*)"/x ) {

   my($length, $dcode, $speed) = ($1, $2, $3);

   print "len '$length'   code '$dcode'   speed '$speed'\n";
}
------------


You can adapt the above for the other tag too.


--
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


------------------------------

Date: 18 Jul 2000 05:50:03 GMT
From: John_member@newsguy.com.bbs@openbazaar.net (John Casey)
Subject: Reading/modifying file - perl script help
Message-Id: <3bRd5Q$Uy7@openbazaar.net>

Problem :

      I have a sql script which creates Tables and Indexes.   I have
another file(say config file) which contains the details of the tables in the
following format:
 
      Tablename    Initialextent   NextExtent   PctIncrease
 
       EMP_TAB        10 m           5 m           0
       SAL_TAB        100 m          10 m          10
       GRD_TAB         200 m         25 m          0
       .......


I need to write a perl script which will "sync" the  initial/next/pctincrease
values of all the tables in the sql script as per the values in the config file.
(that is because i run the sql script on
systems of different disk capacities)

For eg:  the script needs to scan the sql file for  "create table statements"
and then get to the corresponding "storage clause of the statement" before
checking, lookingup in the config file and changing(if needed) in the sql file.
 
         create table EMP_TAB {
            OID int not null,
            Name varchar(255) null,
            ....
         }
         storage (initial 5 m  next 1 m pctincrease 50) tablespace empdata;

          would be changed to
 
            create table EMP_TAB {
               //*all the stuff same as above *//
            }
            storage (initial 10 m next 5 m pctincrease 0) tablespace empdata ;


          Any hints on the perl script?  I have been trying to work it out
as follows ..

****
open(SQLFILE, "sqlfilename") || die "cannot open sqlfile \n";
open (CFGFILE, "configfilename") || die "cannot open config file \n";
while (<SQLFILE>)
{
  chomp;
  if (/create \s+ table/)
  {
      my $tab = $_ ;
      $line = <SQLFILE>   until ( ($line =~ /storage/) || ($line =~ /;/) );
      if ($line =~ /storage/)
      {
        $line =~ /storage \s+ initial (\d+) k next (\d+) k pctincrease (\d+))/;
        my $initial = $1;
        my $next = $2;
        my $pcti = $3;
        $tab =~ /create table (\w+) \(/ ;
        my $TABLE_NAME = $1 ;
        @table_data = `grep $TABLE_NAME tbdata` ;
        chomp(@table_data);
        ($tblname,$initial_new,$next_new,$pcti_new,$junk) = split(/\s+/,@table)

*****

    And here is where i am wondering how do i write the new values back into
the file.  That is how do i modify the values in the file on fly?
Also, is the above approach correct at all?

Any help is appreciated
thanks
John


------------------------------

Date: 11 Jul 2000 13:20:01 GMT
From: joerg@sql.de.bbs@openbazaar.net ()
Subject: Redirect External Program's output on Windows ?
Message-Id: <3bMSG1$VSL@openbazaar.net>

Dear Perl users,

I ask for hints when porting a Perl program from Unix to Win32:

I need to call external programs that write to their "standard
output", but I need to keep that text in files.
I would prefer to include "standard error" with the output.

On Unix, I call "system" with a string parameter that contains
redirection characters:
   system "program arg1 arg2 > result"
or even
   system "program arg1 arg2 > result 2>&1"
which does exactly what I want.

On Windows NT 4.0 (service pack 5), this fails with both
- "cmd.exe": complains about "syntax error"
- "zsh": passes both ">" and "result" as 3rd + 4th arg to "program"
  (I had to re-write the call to
      system "program" , "arg1 arg2 > result"
  because the original line had used "program" twice - as the
  binary to execute, and as an additional first parameter!)


What is the best way to achieve the desired effect ?

AFAIK, "cmd" does support redirection of standard output;
"zsh" definitely does (and it works when using the command line).

Is it a known effect that the "program" must be separated
from the arguments when using "system" on Windows ?
(It works as one string on Unix and in
   open ( FILE, "program args |" )
but fails in "system".)


Additional information:
- I use "ActivePerl" 5.6.0
- The programs to be called are generated in a MinGW environment,
  they are ".exe" binaries.
- I use the "PERL5SHELL" environment variable to set the command
  interpreter to use.

I post via Deja because my local server does not carry this group;
please keep the title so that I can search for your answers
(or even mail me a copy - thanks!).

Regards and Thanks,
Joerg Bruehe

--
Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany
     (speaking only for himself)
mailto: joerg@sql.de


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: 11 Jul 2000 13:20:01 GMT
From: joerg@sql.de.bbs@openbazaar.net ()
Subject: Redirect External Program's output on Windows ?
Message-Id: <3bMSG2$VWM@openbazaar.net>

Dear Perl users,

I ask for hints when porting a Perl program from Unix to Win32:

I need to call external programs that write to their "standard
output", but I need to keep that text in files.
I would prefer to include "standard error" with the output.

On Unix, I call "system" with a string parameter that contains
redirection characters:
   system "program arg1 arg2 > result"
or even
   system "program arg1 arg2 > result 2>&1"
which does exactly what I want.

On Windows NT 4.0 (service pack 5), this fails with both
- "cmd.exe": complains about "syntax error"
- "zsh": passes both ">" and "result" as 3rd + 4th arg to "program"
  (I had to re-write the call to
      system "program" , "arg1 arg2 > result"
  because the original line had used "program" twice - as the
  binary to execute, and as an additional first parameter!)


What is the best way to achieve the desired effect ?

AFAIK, "cmd" does support redirection of standard output;
"zsh" definitely does (and it works when using the command line).

Is it a known effect that the "program" must be separated
from the arguments when using "system" on Windows ?
(It works as one string on Unix and in
   open ( FILE, "program args |" )
but fails in "system".)


Additional information:
- I use "ActivePerl" 5.6.0
- The programs to be called are generated in a MinGW environment,
  they are ".exe" binaries.
- I use the "PERL5SHELL" environment variable to set the command
  interpreter to use.

I post via Deja because my local server does not carry this group;
please keep the title so that I can search for your answers
(or even mail me a copy - thanks!).

Regards and Thanks,
Joerg Bruehe

--
Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany
     (speaking only for himself)
mailto: joerg@sql.de


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: 11 Jul 2000 13:20:01 GMT
From: joerg@sql.de.bbs@openbazaar.net ()
Subject: Redirect External Program's output on Windows ?
Message-Id: <3bMSG3$VaN@openbazaar.net>

Dear Perl users,

I ask for hints when porting a Perl program from Unix to Win32:

I need to call external programs that write to their "standard
output", but I need to keep that text in files.
I would prefer to include "standard error" with the output.

On Unix, I call "system" with a string parameter that contains
redirection characters:
   system "program arg1 arg2 > result"
or even
   system "program arg1 arg2 > result 2>&1"
which does exactly what I want.

On Windows NT 4.0 (service pack 5), this fails with both
- "cmd.exe": complains about "syntax error"
- "zsh": passes both ">" and "result" as 3rd + 4th arg to "program"
  (I had to re-write the call to
      system "program" , "arg1 arg2 > result"
  because the original line had used "program" twice - as the
  binary to execute, and as an additional first parameter!)


What is the best way to achieve the desired effect ?

AFAIK, "cmd" does support redirection of standard output;
"zsh" definitely does (and it works when using the command line).

Is it a known effect that the "program" must be separated
from the arguments when using "system" on Windows ?
(It works as one string on Unix and in
   open ( FILE, "program args |" )
but fails in "system".)


Additional information:
- I use "ActivePerl" 5.6.0
- The programs to be called are generated in a MinGW environment,
  they are ".exe" binaries.
- I use the "PERL5SHELL" environment variable to set the command
  interpreter to use.

I post via Deja because my local server does not carry this group;
please keep the title so that I can search for your answers
(or even mail me a copy - thanks!).

Regards and Thanks,
Joerg Bruehe

--
Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany
     (speaking only for himself)
mailto: joerg@sql.de


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: 11 Jul 2000 13:20:01 GMT
From: joerg@sql.de.bbs@openbazaar.net ()
Subject: Redirect External Program's output on Windows ?
Message-Id: <3bMSG4$VeG@openbazaar.net>

Dear Perl users,

I ask for hints when porting a Perl program from Unix to Win32:

I need to call external programs that write to their "standard
output", but I need to keep that text in files.
I would prefer to include "standard error" with the output.

On Unix, I call "system" with a string parameter that contains
redirection characters:
   system "program arg1 arg2 > result"
or even
   system "program arg1 arg2 > result 2>&1"
which does exactly what I want.

On Windows NT 4.0 (service pack 5), this fails with both
- "cmd.exe": complains about "syntax error"
- "zsh": passes both ">" and "result" as 3rd + 4th arg to "program"
  (I had to re-write the call to
      system "program" , "arg1 arg2 > result"
  because the original line had used "program" twice - as the
  binary to execute, and as an additional first parameter!)


What is the best way to achieve the desired effect ?

AFAIK, "cmd" does support redirection of standard output;
"zsh" definitely does (and it works when using the command line).

Is it a known effect that the "program" must be separated
from the arguments when using "system" on Windows ?
(It works as one string on Unix and in
   open ( FILE, "program args |" )
but fails in "system".)


Additional information:
- I use "ActivePerl" 5.6.0
- The programs to be called are generated in a MinGW environment,
  they are ".exe" binaries.
- I use the "PERL5SHELL" environment variable to set the command
  interpreter to use.

I post via Deja because my local server does not carry this group;
please keep the title so that I can search for your answers
(or even mail me a copy - thanks!).

Regards and Thanks,
Joerg Bruehe

--
Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany
     (speaking only for himself)
mailto: joerg@sql.de


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: 14 Jul 2000 15:40:03 GMT
From: joerg@sql.de.bbs@openbazaar.net ()
Subject: Re: Redirect External Program's output on Windows ?
Message-Id: <3bP0N4$Wbv@openbazaar.net>

Greetings !

In article <8kfdsv$mqb$1@nnrp1.deja.com>,
  undergronk@yahoo.com wrote:
> In article <8kf6j5$gmr$1@nnrp1.deja.com>,
>   joerg@sql.de wrote:
[ = I myself, J.B.]
> > [...]
> >
> > I need to call external programs that write to their "standard
> > output", but I need to keep that text in files.
> > I would prefer to include "standard error" with the output.

I just found out this is a "must", not just a "like to" ;-)

> >
> > On Unix, I call "system" with a string parameter that contains
> > redirection characters:
> > [...]
> >    system "program arg1 arg2 > result 2>&1"
> > which does exactly what I want.
> <snip.
> > What is the best way to achieve the desired effect ?
>
> You could use backticks to capture all the output of the program and
> then print that, if you don't mind STDOUT and STDERR mixed together
>
> $output = `someprogram.exe`;
> print $output;

I tried that - it collects STDOUT fine, but STDERR still goes
to the console and so is lost.

My environment is this:

I have a set of test program sources. The Perl script is to
compile, link, and then run each of these programs.
I need to store the STDOUT and STDERR (several of these tests
intentionally  produce run time error messages) into a protocol file
specific for the test case.
Later, I will collect these files from several machines (various
Unixes and one Win-NT) and compare them.

If there is no way to specifically collect STDERR of an external
program on WinNT (it seems like - neither backticks nor
   open ( FILE, "program |" )
got STDERR in my tests),
can I dynamically change the STDERR destination of the running Perl
program for each test case and then let the test object inherit this
setting ?

Thanks for all hints,
Joerg Bruehe

--
Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany
     (speaking only for himself)
mailto: joerg@sql.de


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: 17 Jul 2000 14:30:01 GMT
From: joerg@sql.de.bbs@openbazaar.net ()
Subject: Re: Redirect External Program's output on Windows ?
Message-Id: <3bRF7P$SmO@openbazaar.net>

Greetings !

I post this result message in order to get it archived
for all those later having a similar problem ...

In article <8knboc$guc$1@nnrp1.deja.com>,
  joerg@sql.de  [ = I myself ] wrote:
>
> [...]
>
> I have a set of test program sources. The Perl script is to
> compile, link, and then run each of these programs.
> I need to store the STDOUT and STDERR (several of these tests
> intentionally  produce run time error messages) into a protocol file
> specific for the test case.

In Bourne-shell syntax that is
   $test_program > $test_protocol 2>&1

On Unix platforms, you can build that up in a string and simply
call "system" with this string - it works as desired.

On Win platforms (Win NT 4, with "ActivePerl" 5.6, trying both
"cmd.exe" and the "zsh" via "$PERL5SHELL"), this redirection fails.

My (simplified) solution to overcome this Win deficiency is:

    open STDERR, "> $errfile" ; # specific for test
    $output_text = `$test_prog` ;
    $rc = $? ;

    open STDERR, ">&STDOUT" ; # reset for Perl script
    if ( ! open ( FILE_ERR , $errfile ) ) {
        warn "Cannot open '$errfile'" ; }
    else {
        $output_text .= join ( "", <FILE_ERR> );
        close ( FILE_ERR ); }
    unlink ( "$errfile" );

Now "$output_text" contains the program's stdout followed
by its stderr, that can be analyzed, written to file etc.


Explicitly, I want to state that the backticks just provide
"standard output" of the external program, not "standard error".

As regards the proposal to use "open3": Thank you, but in
"Programming Perl" it is explicitly stated (2nd edition, p. 344):
  "As it is, the Open2 and Open3 modules are unlikely
  to work anywhere except on a UNIX system, or some
  other system purporting to be POSIX compliant."
And I simply do not trust any NT claims to be POSIX compliant ;-)


Again, thank you for all your hints !

Joerg Bruehe

--
Joerg Bruehe, SQL Datenbanksysteme GmbH, Berlin, Germany
     (speaking only for himself)
mailto: joerg@sql.de


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V9 Issue 3727
**************************************


home help back first fref pref prev next nref lref last post