Re: File::Sluprer (Re: Multi line file handling)

2015-10-20 Thread Lars Noodén
On 10/19/2015 07:40 PM, Kent Fredric wrote:
> On 20 October 2015 at 03:55, Lars Noodén  wrote:
>> What is the main reason for the preference of File::Slurper over
>> File::Slurp these days?
> 
> 
> http://blogs.perl.org/users/leon_timmermans/2015/08/fileslurp-is-broken-and-wrong.html

Thanks.  That clarifies things and points to File::Slurper, Path::Tiny,
and IO::All as alternatives.

Regards,
Lars

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




Multi line file handling

2015-10-19 Thread Prashant Thorat
Hi All,

I have a file with multiple lines ,I want to assign all content to variable
& then want to work on it.
Like want to match pattern /window/ from it.
how can it is possible ?
for example -

open ( F1 ,"file.txt") || die "$!";

$a=;

if (/window/i){print
"it is present\n";
}


Re: Multi line file handling

2015-10-19 Thread Ian
Take a look at File::Slurp.

Regards

On Mon, Oct 19, 2015 at 9:18 AM, Prashant Thorat 
wrote:

> Hi All,
>
> I have a file with multiple lines ,I want to assign all content to
> variable & then want to work on it.
> Like want to match pattern /window/ from it.
> how can it is possible ?
> for example -
>
> open ( F1 ,"file.txt") || die "$!";
>
> $a=;
>
> if (/window/i){print
> "it is present\n";
> }
>
>
>


-- 
Ian
http://www.cicsworld.com


Re: Multi line file handling

2015-10-19 Thread Brandon McCaig
On Mon, Oct 19, 2015 at 07:48:06PM +0530, Prashant Thorat wrote:
> Hi All,

Hello,

> I have a file with multiple lines ,I want to assign all content to variable
> & then want to work on it.
> Like want to match pattern /window/ from it.
> how can it is possible ?
> for example -
> 
> open ( F1 ,"file.txt") || die "$!";

You should always use strict and use warnings. They will catch a
lot of gotchas that Perl will normally silently ignore.

use strict;
use warnings;

You should prefer the 3-argument open() as a best practice
because it's safer. Also, you should prefer lexical file handles.

Note that there's no need to quote a variable in Perl (Perl is
not a shell language; variable quoting is not necessary). die $!
would suffice. That said, you should probably add some context to
that error so that you know what failed.

For example:

open my $fh, '<', 'file.txt' or die "open: $!";

> $a=;

You should avoid using $a or $b as variable names because they
have special meaning (i.e., see perldoc -f sort).

See `perldoc perlvar' for $/, the input record separator (AKA
$INPUT_RECORD_SEPARATOR with use English). If set to undef then
you can slurp in the whole file with one read.

my $contents = do {
local $/ = undef;
<$fh>;
};

> if (/window/i){print
> "it is present\n";
> }

It's best to avoid using the default variable, $_, because it can
very easily be overwritten from a distance. You originally
planned to put the contents into $a, but as noted above that's a
bad practice. Instead, I read into $contents.

We can match the contents of the file by binding the regex to the
contents:

if ($contents =~ /window/i) {
print "Contents of file.txt matched window.\n";
}

Even though a lexical file handle will automatically be closed
when it goes out of scope it's still a good practice to manually
close it and check for errors.

close $fh or warn "close: $!";

Regards,


-- 
Brandon McCaig  
Castopulence Software 
Blog 
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'



signature.asc
Description: Digital signature


File::Sluprer (Re: Multi line file handling)

2015-10-19 Thread Lars Noodén
On 10/19/2015 05:49 PM, Shlomi Fish wrote:
> Just for the record, using File::Slurp is no longer recommended and one should
> be using https://metacpan.org/pod/Path::Tiny or perhaps
> https://metacpan.org/pod/File::Slurper instead.

What is the main reason for the preference of File::Slurper over
File::Slurp these days?

Regards,
/Lars

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




Re: Multi line file handling

2015-10-19 Thread Shawn H Corey
On Mon, 19 Oct 2015 19:48:06 +0530
Prashant Thorat  wrote:

> Hi All,
> 
> I have a file with multiple lines ,I want to assign all content to
> variable & then want to work on it.
> Like want to match pattern /window/ from it.
> how can it is possible ?
> for example -
> 
> open ( F1 ,"file.txt") || die "$!";
> 
> $a=;
> 
> if (/window/i){print
> "it is present\n";
> }

use autodie;  # dies when I/O error occurs

my $file = 'file.txt';  # write literals once so they are easy
# to find and change
my $contents = '';  # holds contents of $file

# use a block to isolate the INPUT_RECORD_SEPARATOR
{
local $/; # file slurp mode
open my $fh, '<', $file;  # three-argument open
$contents = <$fh>;# slurps the whole file into $contents
close $fh;# in case of input error,
  # give it a chance to report
}

if( $contents =~ /window/i ){
print "it is present\n";
}

# see `perldoc perlvar` and search for /INPUT_RECORD_SEPARATOR
# http://perldoc.perl.org/perlvar.html


-- 
Don't stop where the ink does.
Shawn

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




Re: Multi line file handling

2015-10-19 Thread Shlomi Fish
Hi Ian and Prashant and all,

On Mon, 19 Oct 2015 09:28:15 -0500
Ian  wrote:

> Take a look at File::Slurp.
> 

Just for the record, using File::Slurp is no longer recommended and one should
be using https://metacpan.org/pod/Path::Tiny or perhaps
https://metacpan.org/pod/File::Slurper instead.

Finally, a few notes on the code exercpt:

> Regards
> 
> On Mon, Oct 19, 2015 at 9:18 AM, Prashant Thorat 
> wrote:
> 
> > Hi All,
> >
> > I have a file with multiple lines ,I want to assign all content to
> > variable & then want to work on it.
> > Like want to match pattern /window/ from it.
> > how can it is possible ?
> > for example -
> >
> > open ( F1 ,"file.txt") || die "$!";
> >
> > $a=;
> >
> > if (/window/i){print
> > "it is present\n";
> > }
> >

1. Use three-args-open.

2. Use lexical filehandles.

3. Add "use strict;" and "use warnings;".

4. Don't abuse the "$a" variable.

5. Format/indent the code correctly.

6. You should bind the regular expression match to a lexical variable using:

if ($str =~ /window/i) {
...
}

For more info, see:

http://perl-begin.org/tutorials/bad-elements/

Regards,

Shlomi Fish

> >
> >  
> 
> 



-- 
-
Shlomi Fish   http://www.shlomifish.org/
Beginners Site for the Vim text editor - http://vim.begin-site.org/

 sub id { my $self = shift; $json_parser_for{ $self }
->decode($json_for{ $self })->{id} } # Inside‐out JSON‐notated objects

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

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




Re: Multi line file handling

2015-10-19 Thread Ian
On Mon, Oct 19, 2015 at 9:46 AM, Shlomi Fish  wrote:

> Hi Ian and Prashant and all,
>
> On Mon, 19 Oct 2015 09:28:15 -0500
> Ian  wrote:
>
> > Take a look at File::Slurp.
> >
>
> Just for the record, using File::Slurp is no longer recommended and one
> should
> be using https://metacpan.org/pod/Path::Tiny or perhaps
> https://metacpan.org/pod/File::Slurper instead.
>
>
Thanks Shlomi,

I was not aware. Now I have some homework todo.

Ian
http://www.cicsworld.com


Re: File::Sluprer (Re: Multi line file handling)

2015-10-19 Thread Kent Fredric
On 20 October 2015 at 03:55, Lars Noodén  wrote:
> What is the main reason for the preference of File::Slurper over
> File::Slurp these days?


http://blogs.perl.org/users/leon_timmermans/2015/08/fileslurp-is-broken-and-wrong.html

-- 
Kent

KENTNL - https://metacpan.org/author/KENTNL

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




Re: Perl file handling

2011-09-23 Thread Maggs
On Sep 23, 1:54 am, 2teezp...@gmail.com (timothy adigun) wrote:
 Hello Maggs,

  KS Ken Slater kl...@psu.edu wrote:

 KSOP was using trying to use 'open_file' to open one file handle at a
 time.
 KSArguments were presumably the file handle variable, mode, and  file
 name.
 KSYour example is very different.

 I think 'KS' got the heart of what you really want, and in that light try if
 this following code could help:

 code
 #!/usr/bin/perl -w
 use strict;

 my @arr=();
    my %hash_file=(file_handle='fh',file_mode='mode',filename='name');
     foreach(sort keys %hash_file){
        print Enter your $_: ;
          chomp($hash_file{$_}=STDIN);
       push @arr,$hash_file{$_};
     }
   open_file(@arr);  # call the subroutine

   sub open_file{
     no strict refs;  # use this because filehandle is an expression, that
 is it's value
                                # is the real filehandle, which is considered
 a symbolic ref.
                                # check *perldoc -f open* for more info.
          my ($fh,$mode,$name)=@_;
      open ($fh,$mode,$name) || die can't open $!;
        while($fh){ chomp;
            print $_,\n;   # $_ takes the line you are printing out
        }
      close ($fh) || die can't close file: $!;
   }
 /code

 Regards,
 tim



Hello brandon, am trying to create a simple metasearch engine, so i
have a file that has different words (and links) from different search
engines (e.g. google, yahoo), so what am trying to do is open the file
and retrieve the word which equal to the word entered in the search
text box. The next two subroutines read and write from the file, like
this:

sub read_file{
local ($filevar) = @_;
$filevar;
}

sub read_file{
   local($filevar, $line) = @_;
   print $filevar ($line);
}

so that the results from the files are displayed. So far these two
subroutines dont give any error, its just the one line.


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




Re: Perl file handling

2011-09-23 Thread Uri Guttman
 M == Maggs  maggiechimbwa...@googlemail.com writes:

  M sub read_file{
  M local ($filevar) = @_;

don't use local for that (or for much else). use my. local is meant now
only for special cases with dynamic scoping.

  M $filevar;
  M }

why do you need a whole sub to read a line? just $filevar does the
same thing!

  M sub read_file{
  Mlocal($filevar, $line) = @_;
  Mprint $filevar ($line);
  M }

and you have two subs with the same name doing different things. very
confusing and of course only one will be found by perl. the second one
should be called write_file. in neither case do you show the open
calls.

given what you are calling them, use File::Slurp which has read_file and
write_file subs which do all the work you seem to want here. much
cleaner than your code.

uri

-- 
Uri Guttman  --  uri AT perlhunter DOT com  ---  http://www.perlhunter.com --
  Perl Developer Recruiting and Placement Services  -
-  Perl Code Review, Architecture, Development, Training, Support ---

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




Perl file handling

2011-09-22 Thread Maggs
am trying to open a file but the problem is on the third line
(open...)

sub open_file {
local $/ = undef;
($filevar, $filemode, $filename) = @_;
open ($filevar, $filemode . $filename) || die (Can't open
$filename);
}
everytime i run it i get the error: Can't use string (FILE1) as a
symbol ref while strict refs in use.

Any help please on how to sort this out.


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




Re: Perl file handling

2011-09-22 Thread Brandon McCaig
On Thu, Sep 22, 2011 at 4:54 AM, Maggs maggiechimbwa...@googlemail.com wrote:
 ($filevar, $filemode, $filename) = @_;
 open ($filevar, $filemode . $filename) || die (Can't open
 $filename);
 }
 everytime i run it i get the error: Can't use string (FILE1) as a
 symbol ref while strict refs in use.

$filevar probably contains a value other than undef (apparently
'FILE1'), so open is trying to use its value as a symbolic
reference (something that the strict pragma disallows).

A symbolic reference basically means that the string contains the
name of the variable to use. It's a very obscure and bad
practice, which is why strict disallows it.

Use a new variable or pass an already undef variable into
open_file for the $filevar variable. See perldoc open.

You should also be using the three-argument open instead of
concatenating $filemode and $filename together.

You should actually be able to just pass the arguments to
open_file on to open directly:

  sub open_file
  {
  open @_ or die Can't open '$filename': $!;
  }

  # Call by using my to create $fh with undef, just like with raw
  # open:
  open_file my $fh, '', 'foobar';

  # Or undef the variable first:
  undef $fh;

  open_file $fh, '', 'foobar';

(All of that is untested)

May I ask what the purpose of open_file is anyway? It seems to be
a simple wrapper over open that dies when open fails. Is that all
it does? Perhaps you should see if autodie does what you want?
See perldoc autodie.


-- 
Brandon McCaig http://www.bamccaig.com/ bamcc...@gmail.com
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software http://www.castopulence.org/ bamcc...@castopulence.org

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




Re: Perl file handling

2011-09-22 Thread John W. Krahn

Brandon McCaig wrote:

On Thu, Sep 22, 2011 at 4:54 AM, Maggsmaggiechimbwa...@googlemail.com  wrote:

($filevar, $filemode, $filename) = @_;
open ($filevar, $filemode . $filename) || die (Can't open
$filename);
}
everytime i run it i get the error: Can't use string (FILE1) as a
symbol ref while strict refs in use.


$filevar probably contains a value other than undef (apparently
'FILE1'), so open is trying to use its value as a symbolic
reference (something that the strict pragma disallows).

A symbolic reference basically means that the string contains the
name of the variable to use. It's a very obscure and bad
practice, which is why strict disallows it.

Use a new variable or pass an already undef variable into
open_file for the $filevar variable. See perldoc open.

You should also be using the three-argument open instead of
concatenating $filemode and $filename together.

You should actually be able to just pass the arguments to
open_file on to open directly:

   sub open_file
   {
   open @_ or die Can't open '$filename': $!;


That won't work as the first argument to open is forced into scalar context

$ perl -le'print prototype CORE::open'
*;$@

so if @_ contains three elements that becomes:

   open 3 or die Can't open '$filename': $!;

And why is $filename not passed into the subroutine?




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

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




Re: Perl file handling

2011-09-22 Thread Leo Lapworth
On 22 September 2011 09:54, Maggs maggiechimbwa...@googlemail.com wrote:

 am trying to open a file but the problem is on the third line
 (open...)

 sub open_file {
 local $/ = undef;
 ($filevar, $filemode, $filename) = @_;
 open ($filevar, $filemode . $filename) || die (Can't open
 $filename);
 }


Are you ok installing/using the Path::Class module?  -
https://metacpan.org/module/Path::Class

use Path::Class qw(file);

my $file = file($filename);

my $file_handle = $file-openw();
$file_handle-print(hi);
$file_handle-close();

# or to read
my $file_handle = $file-openr();

# or to get the contents
my $contents = $file-slurp();

print Contents of  . $file-stringify .  are: \n;
print $contents;

Sorry if that's not actually answering exactly what you asked, but maybe
it'll help.

Leo


Re: Perl file handling

2011-09-22 Thread timothy adigun
Hi Maggs,

I don't know what you really wants to achieve with your codes below:

(open...)

sub open_file {
local $/ = undef;
($filevar, $filemode, $filename) = @_;
open ($filevar, $filemode . $filename) || die (Can't open
$filename);
}
everytime i run it i get the error: Can't use string (FILE1) as a
symbol ref while strict refs in use.

Any help please on how to sort this out.

I suppose you want to want to open some files using a subroutine, see if the
code below helps, and if not you may have to explain what you really want to
do.

codes
#!/usr/bin/perl -w
use strict;

open_file(@ARGV);  # call the subroutine

  sub open_file{
foreach (@_){  # iterate on the files to open
 open FH,,$_ || die can't open; # $_ takes the name of the file to
open
   while(FH){ chomp;
   print $_,\n;   # $_ is the line you are printing out
   }
 close FH || die can't close file;
}
  }
/codes
Note: You can call the FH, $fh if you like, but then you must use *my* with
strict in use. You can also use different names for $_, that depends on you.
Regards,
tim


RE: Perl file handling

2011-09-22 Thread Ken Slater


 -Original Message-
 From: timothy adigun [mailto:2teezp...@gmail.com]
 Sent: Thursday, September 22, 2011 2:05 PM
 To: Maggs
 Cc: beginners@perl.org
 Subject: Re: Perl file handling
 
 Hi Maggs,
 
 I don't know what you really wants to achieve with your codes below:
 
 (open...)
 
 sub open_file {
 local $/ = undef;
 ($filevar, $filemode, $filename) = @_;
 open ($filevar, $filemode . $filename) || die (Can't open $filename);
 }
 everytime i run it i get the error: Can't use string (FILE1) as a
 symbol ref while strict refs in use.
 
 Any help please on how to sort this out.
 
 I suppose you want to want to open some files using a subroutine, see
 if the
 code below helps, and if not you may have to explain what you really
 want to
 do.
 
 codes
 #!/usr/bin/perl -w
 use strict;
 
 open_file(@ARGV);  # call the subroutine
 
   sub open_file{
 foreach (@_){  # iterate on the files to open
  open FH,,$_ || die can't open; # $_ takes the name of the
 file to
 open
while(FH){ chomp;
print $_,\n;   # $_ is the line you are printing out
}
  close FH || die can't close file;
 }
   }
 /codes
 Note: You can call the FH, $fh if you like, but then you must use *my*
 with
 strict in use. You can also use different names for $_, that depends on
 you.
 Regards,
 tim

OP was using trying to use 'open_file' to open one file handle at a time.
Arguments were presumably the file handle variable, mode, and  file name.
Your example is very different.

Your code has a precedence problem on the open call.

open FH,,$_ || die can't open;

Try this on a file that does not exist and see what happens.

You need to either place parentheses around the arguments to open or use the
'or' operator.
The '||' operator has higher precedence than the comma. Therefore, it will
only die when $_ evaluates to false.

Also, you should include the reason for the open failure in the message used
by die ($! Or $^E).

HTH, Ken





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




Re: Perl file handling

2011-09-22 Thread Brandon McCaig
On Thu, Sep 22, 2011 at 11:09 AM, John W. Krahn jwkr...@shaw.ca wrote:
 Brandon McCaig wrote:
       open @_ or die Can't open '$filename': $!;

 That won't work as the first argument to open is forced into scalar context

 $ perl -le'print prototype CORE::open'
 *;$@

 so if @_ contains three elements that becomes:

       open 3 or die Can't open '$filename': $!;

I didn't think of that. Still, you could pass them directly on
with $_[0], $_[1], and $_[2] (so at least the alias to $_[0] is
intact, though admittedly I know very little of how aliases work
in Perl). :\

 And why is $filename not passed into the subroutine?

An oversight on my part. :) That's what happens when you don't
test the code. ;) Should have been $_[2].


-- 
Brandon McCaig http://www.bamccaig.com/ bamcc...@gmail.com
V zrna gur orfg jvgu jung V fnl. Vg qbrfa'g nyjnlf fbhaq gung jnl.
Castopulence Software http://www.castopulence.org/ bamcc...@castopulence.org

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




Re: Perl file handling

2011-09-22 Thread timothy adigun
Hello Maggs,

 KS Ken Slater kl...@psu.edu wrote:

KSOP was using trying to use 'open_file' to open one file handle at a
time.
KSArguments were presumably the file handle variable, mode, and  file
name.
KSYour example is very different.

I think 'KS' got the heart of what you really want, and in that light try if
this following code could help:

code
#!/usr/bin/perl -w
use strict;

my @arr=();
   my %hash_file=(file_handle='fh',file_mode='mode',filename='name');
foreach(sort keys %hash_file){
   print Enter your $_: ;
 chomp($hash_file{$_}=STDIN);
  push @arr,$hash_file{$_};
}
  open_file(@arr);  # call the subroutine

  sub open_file{
no strict refs;  # use this because filehandle is an expression, that
is it's value
   # is the real filehandle, which is considered
a symbolic ref.
   # check *perldoc -f open* for more info.
 my ($fh,$mode,$name)=@_;
 open ($fh,$mode,$name) || die can't open $!;
   while($fh){ chomp;
   print $_,\n;   # $_ takes the line you are printing out
   }
 close ($fh) || die can't close file: $!;
  }
/code

Regards,
tim


Re: file handling

2010-02-19 Thread elavazhagan perl
Guys,

Here I come with the code for the dynamic file update.
Please let me know your comments and concerns with this code.
Thanks for your support and comments.

#

#! c:/perl/bin/perl
use File::Copy;

my $file = shift;
my $old = $file;
my $new = $file.tmp.$$;
my $bak = $file.bak;

copy($old, $new) or die can't copy the file:$!;
open(OLD,  $old) or die can't open $old: $!;
open(NEW,  $new) or die can't open $new: $!;
print new file ,$new,\n\n;

$count=1000;

# Correct typos, preserving case
for($i=1;$i=$count;$i++) {

 seek(OLD,0,0);
 while ( OLD ) {

  #Condition to match the Field ID and incremanet by 20.
  #Field ID=23120
  if ( s/(Field.*)(\d+)/ $1 . ( $2 + ($i*20) ) . '' /e )
  {
   print OLD:$2 \t NEW:, $2 + ($i*20), \n;
  }

  #Condition to match the line and increament by 1.
  # string ID=Name Value=24G_L24_UnitsQualifier_2123 /
  if ( s/_L(\d{1,3})_/ '_L' . ( $1 + ($i*1) ) . '_' /e )
  {
   print OLD:L$1 \t NEW:L, $1 + ($i*1), \n;
  }

  #Condition to increment the final segment in the above string
  if ( s/(string.*L\d+.*_)(\d+)/ $1 . ( $2 + ($i*20) ) . '' /e )
  {
   print OLD:$2 \t NEW:, $2 + ($i*20), \n;
  }

  print NEW $_ or die can't write to $new: $!;

 }
 print NEW \n;


}

close(OLD)  or die can't close $old: $!;
close(NEW)  or die can't close $new: $!;


rename($old, $bak)  or die can't rename $old to $bak: $!;
rename($new, $old)  or die can't rename $new to $old: $!;



#

THANKS.



On 2/12/10, Robert Wohlfarth rbwohlfa...@gmail.com wrote:

 On Fri, Feb 12, 2010 at 3:32 AM, elavazhagan perl 
 elavazhagan.p...@gmail.com wrote:

 *ORIGINAL CONTENT:*
 Field ID=*2*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L1*_Valid_Line_Indicator_*2* /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field

 *DESIRED OUTPUT:*

  Field ID=*20980*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L1000*_Valid_Line_Indicator_*20980*
 /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field


 In summary, you want to...

1. Read an entire file into one string.
2. Using a regular expression, get the current value of Field ID.
3. Loop from 2 to 1000...
   1. Increment Field ID by 20.
   2. Substitute the new Field ID for the old one, using regular
   expressions.
   3. Substitute the counter (L) in the string ID=Name field.
   4. Write the entire block out to a new file.

 Is that accurate?

 --
 Robert Wohlfarth




Re: file handling

2010-02-12 Thread elavazhagan perl
Hope,this time I will make you clear.

Our script converts the original content to  current output ,what we need is
the desired output.

*ORIGINAL CONTENT:*
Field ID=*2*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L1*_Valid_Line_Indicator_*2* /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field

*CURRENT OUTPUT:*

Field ID=*20020*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L2*_Valid_Line_Indicator_*20020* /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field

*DESIRED OUTPUT:*

 Field ID=*20980*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L1000*_Valid_Line_Indicator_*20980* /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field


Thanks.


On 2/12/10, John W. Krahn jwkr...@shaw.ca wrote:

 elavazhagan perl wrote:

 On 2/11/10, John W. Krahn jwkr...@shaw.ca wrote:


 elavazhagan perl wrote:


 I have a program which reads the input file,updates once and outputs in
 a new file.
 Ex.The first line L1 will be updated to L2.
 I need to put this into a loop so that the scripts will be invoked 1000
 times and the output contains the appended data(L1 TO L1000).
 Please can you suggest few methods.

 It is not clear from your description or from your code exactly what you
 want to do.  Do you want 1,000 separate files or one file with the data
 replicated 1,000 times or something else?


 I will try my best to make you clear.
 Once we execute the script,it should generate the data for the 1000
 lines.Now the script just converts the data for a single line


 The data you provided has 156 lines in it.  Do you consider all that data
 to be just one line?

 i.e Line1 will
 be converted to line 2 and some increaments.
 In order to get my desired data,I need to execute the script 1000
 times,and
 each output will be appended in a new file which will containthe whole
 data.
 I am trying to update the script so that for a single execution,it will
 produce the whole data.
 Hope I make it clear.Please let me know if any ambiguityon this.


 What you want sounds easy enough to do.  If you could just clear up what
 you mean by line?




 John
 --
 The programmer is fighting against the two most
 destructive forces in the universe: entropy and
 human stupidity.   -- Damian Conway

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





Fwd: file handling

2010-02-12 Thread elavazhagan perl
Hi,

Please find the correction in my words.
The script should read the original content and update and append the data
to a new file upto the desired output.

Thanks.

-- Forwarded message --
From: elavazhagan perl elavazhagan.p...@gmail.com
Date: Feb 12, 2010 3:02 PM
Subject: Re: file handling
To: John W. Krahn jwkr...@shaw.ca
Cc: beginners@perl.org



Hope,this time I will make you clear.

Our script converts the original content to  current output ,what we need is
the desired output.

*ORIGINAL CONTENT:*
Field ID=*2*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L1*_Valid_Line_Indicator_*2* /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field

*CURRENT OUTPUT:*

Field ID=*20020*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L2*_Valid_Line_Indicator_*20020* /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field

*DESIRED OUTPUT:*

 Field ID=*20980*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L1000*_Valid_Line_Indicator_*20980* /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field


Thanks.


On 2/12/10, John W. Krahn jwkr...@shaw.ca wrote:

 elavazhagan perl wrote:

 On 2/11/10, John W. Krahn jwkr...@shaw.ca wrote:


 elavazhagan perl wrote:


 I have a program which reads the input file,updates once and outputs in
 a new file.
 Ex.The first line L1 will be updated to L2.
 I need to put this into a loop so that the scripts will be invoked 1000
 times and the output contains the appended data(L1 TO L1000).
 Please can you suggest few methods.

 It is not clear from your description or from your code exactly what you
 want to do.  Do you want 1,000 separate files or one file with the data
 replicated 1,000 times or something else?


 I will try my best to make you clear.
 Once we execute the script,it should generate the data for the 1000
 lines.Now the script just converts the data for a single line


 The data you provided has 156 lines in it.  Do you consider all that data
 to be just one line?

 i.e Line1 will
 be converted to line 2 and some increaments.
 In order to get my desired data,I need to execute the script 1000
 times,and
 each output will be appended in a new file which will containthe whole
 data.
 I am trying to update the script so that for a single execution,it will
 produce the whole data.
 Hope I make it clear.Please let me know if any ambiguityon this.


 What you want sounds easy enough to do.  If you could just clear up what
 you mean by line?




 John
 --
 The programmer is fighting against the two most
 destructive forces in the universe: entropy and
 human stupidity.   -- Damian Conway

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





Re: file handling

2010-02-12 Thread John W. Krahn

elavazhagan perl wrote:

Hope,this time I will make you clear.

Our script converts the original content to  current output ,what we need is
the desired output.


So I take it by line you actually mean Field record?



*ORIGINAL CONTENT:*
Field ID=*2*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L1*_Valid_Line_Indicator_*2* /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field

*CURRENT OUTPUT:*

Field ID=*20020*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L2*_Valid_Line_Indicator_*20020* /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field

*DESIRED OUTPUT:*

 Field ID=*20980*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L1000*_Valid_Line_Indicator_*20980* /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field


The original 'Field ID' starts out at 2 and ends up at 20980.  If 
you increment that by 20 each time then that is 49 changes to 'Field 
ID'.  The 'string ID' value starts out at 1 and ends up at 1000 which is 
999 changes.  If both values are changed at the same time then they 
should both have the same number of changes.  So how do you determine 
when to change one value and not the other?




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.   -- Damian Conway

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




Re: file handling

2010-02-12 Thread elavazhagan perl
Yes, you are absolutely right..
We need to increment the line by one  and field ID by 20.
Whatever my be the fied id..
Thanks,

On 2/12/10, John W. Krahn jwkr...@shaw.ca wrote:

 elavazhagan perl wrote:

 Hope,this time I will make you clear.

 Our script converts the original content to  current output ,what we need
 is
 the desired output.


 So I take it by line you actually mean Field record?


 *ORIGINAL CONTENT:*

 Field ID=*2*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L1*_Valid_Line_Indicator_*2* /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field

 *CURRENT OUTPUT:*

 Field ID=*20020*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L2*_Valid_Line_Indicator_*20020* /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field

 *DESIRED OUTPUT:*

  Field ID=*20980*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L1000*_Valid_Line_Indicator_*20980*
 /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field


 The original 'Field ID' starts out at 2 and ends up at 20980.  If you
 increment that by 20 each time then that is 49 changes to 'Field ID'.  The
 'string ID' value starts out at 1 and ends up at 1000 which is 999 changes.
  If both values are changed at the same time then they should both have the
 same number of changes.  So how do you determine when to change one value
 and not the other?




 John
 --
 The programmer is fighting against the two most
 destructive forces in the universe: entropy and
 human stupidity.   -- Damian Conway

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





Re: file handling

2010-02-12 Thread John W. Krahn

elavazhagan perl wrote:

Yes, you are absolutely right..
We need to increment the line by one  and field ID by 20.
Whatever my be the fied id..
Thanks,


OK, I give up.  Does anyone else want to try eliciting a cogent and 
informative response?



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.   -- Damian Conway

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




Re: file handling

2010-02-12 Thread Robert Wohlfarth
On Fri, Feb 12, 2010 at 3:32 AM, elavazhagan perl 
elavazhagan.p...@gmail.com wrote:

 *ORIGINAL CONTENT:*
 Field ID=*2*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L1*_Valid_Line_Indicator_*2* /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field

 *DESIRED OUTPUT:*

  Field ID=*20980*
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_*L1000*_Valid_Line_Indicator_*20980* /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field


In summary, you want to...

   1. Read an entire file into one string.
   2. Using a regular expression, get the current value of Field ID.
   3. Loop from 2 to 1000...
  1. Increment Field ID by 20.
  2. Substitute the new Field ID for the old one, using regular
  expressions.
  3. Substitute the counter (L) in the string ID=Name field.
  4. Write the entire block out to a new file.

Is that accurate?

-- 
Robert Wohlfarth


Re: file handling

2010-02-11 Thread John W. Krahn

elavazhagan perl wrote:

Hi,


Hello,


 I have a program which reads the input file,updates once and outputs in a
new file.
Ex.The first line L1 will be updated to L2.
I need to put this into a loop so that the scripts will be invoked 1000
times and the output contains the appended data(L1 TO L1000).
Please can you suggest few methods.


It is not clear from your description or from your code exactly what you 
want to do.  Do you want 1,000 separate files or one file with the data 
replicated 1,000 times or something else?


Anyway, let's have a look at the code:

[ Note that you should indent your code consistently to aide in 
readability. ]



#! c:/perl/bin/perl

use strict;

my $file = shift;
my $old = $file;
my $new = $file.tmp.$$;
my $bak = $file.bak;

#File Handling
open(OLD,  $old) or die can't open $old: $!;
open(NEW,  $new) or die can't open $new: $!;

# Correct typos, preserving case
while (OLD) {
#Condition to match the Field ID and incremanet by 20.
#Field ID=23120
if($_ =~ /\Field(.*)\(.*)\\/)


Why are you capturing to $1 when you are not using it?


{
my $val= $2;
my $newval=$val+20;
$_ =~ s/$val/$newval/;


You shouldn't do that.  See below for explanation.

print OLD:$val \t NEW:$newval\n ; 
}


#Condition to match the line and increament by 1.
# string ID=Name Value=24G_L24_UnitsQualifier_2123 /
if ($_ =~ /(L\d{2,3})\_/g)


Why are you using the /g global option if the pattern only occurs once 
in the string?  The data you supplied does not match the pattern 
/(L\d{2,3})_/ so is the data wrong or the pattern wrong or something else?



  {
 my $line = $1;
if($line =~ /(\d+)/g)


Why are you matching \d again when you already know that \d exists in 
the string?  Why are you using the /g global option if the pattern only 
occurs once in the string?



{
my $n_line = $1;
my $newline = $n_line+1;
$_=~ s/$line/L$newline/;


You *really* shouldn't do that.


print OLD:$line \t NEW:L$newline\n ;
}

 }
 #Condition to increment the final segment in the above string
  if($_=~ /\string(.*)L(\d+)(.*)\_(\d+)\/)


Why are you capturing to $1, $2 and $3 when you are not using them?


 {
 my $value=$4;
 my $newvalue=$value+20;
 $_=~ s/$value/$newvalue/;


You are running a pattern match using a string (in this case $value) 
that may occur anywhere in the string, not just the place where you 
originally found it, and may contain regular expression meta-characters 
that could cause the pattern to fail.


 print OLD:$value \t NEW:$newvalue\n ; 
 }
 
(print NEW $_)  or die can't write to $new: $!;

}


close(OLD)  or die can't close $old: $!;
close(NEW)  or die can't close $new: $!;
rename($old, $bak)  or die can't rename $old to $bak: $!;
rename($new, $old)  or die can't rename $new to $old: $!;



Your while loop would probably be better written as:

# Correct typos, preserving case
while ( OLD ) {
#Condition to match the Field ID and incremanet by 20.
#Field ID=23120
if ( s/(Field.*)(\d+)/ $1 . ( $2 + 20 ) . '' /e )
{
print OLD:$2 \t NEW:, $2 + 20, \n;
}

#Condition to match the line and increament by 1.
# string ID=Name Value=24G_L24_UnitsQualifier_2123 /
if ( s/_L(\d{2,3})_/ '_L' . ( $1 + 1 ) . '_' /e )
{
print OLD:L$1 \t NEW:L, $1 + 1, \n;
}

#Condition to increment the final segment in the above string
if ( s/(string.*L\d+.*_)(\d+)/ $1 . ( $2 + 20 ) . '' /e )
{
print OLD:$2 \t NEW:, $2 + 20, \n;
}

print NEW $_ or die can't write to $new: $!;
}





John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.   -- Damian Conway

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




Re: file handling

2010-02-11 Thread John W. Krahn

elavazhagan perl wrote:

On 2/11/10, John W. Krahn jwkr...@shaw.ca wrote:


elavazhagan perl wrote:


I have a program which reads the input file,updates once and outputs in
a new file.
Ex.The first line L1 will be updated to L2.
I need to put this into a loop so that the scripts will be invoked 1000
times and the output contains the appended data(L1 TO L1000).
Please can you suggest few methods.


It is not clear from your description or from your code exactly what you
want to do.  Do you want 1,000 separate files or one file with the data
replicated 1,000 times or something else?


I will try my best to make you clear.
Once we execute the script,it should generate the data for the 1000
lines.Now the script just converts the data for a single line


The data you provided has 156 lines in it.  Do you consider all that 
data to be just one line?



i.e Line1 will
be converted to line 2 and some increaments.
In order to get my desired data,I need to execute the script 1000 times,and
each output will be appended in a new file which will containthe whole
data.
I am trying to update the script so that for a single execution,it will
produce the whole data.
Hope I make it clear.Please let me know if any ambiguityon this.


What you want sounds easy enough to do.  If you could just clear up what 
you mean by line?




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.   -- Damian Conway

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




file handling

2010-02-10 Thread elavazhagan perl
Hi,

 I have a program which reads the input file,updates once and outputs in a
new file.
Ex.The first line L1 will be updated to L2.
I need to put this into a loop so that the scripts will be invoked 1000
times and the
output contains the appended data(L1 TO L1000).
Please can you suggest few methods.

Thanks.
neelike.
 Field ID=2
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_L1_Valid_Line_Indicator_2 /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field
 Field ID=20001
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=42_L1_Revenue_Code_20001 /
boolean ID=LinkedCoord Value=true /
integer ID=LinkedFieldId Value=290 /
integer ID=LinkedLineId Value=0 /
   /AttrSet
 /Field
 Field ID=20003
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=43_L1_Revenue_Description_20003 /
boolean ID=LinkedCoord Value=true /
integer ID=LinkedFieldId Value=295 /
integer ID=LinkedLineId Value=0 /
   /AttrSet
 /Field
 Field ID=20004
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=44_L1_HCPCS_Rates_Flag_20004 /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field
 Field ID=20005
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=44_L1_HCPCS_Rates_Codes_20005 /
boolean ID=LinkedCoord Value=true /
integer ID=LinkedFieldId Value=300 /
integer ID=LinkedLineId Value=0 /
   /AttrSet
 /Field
 Field ID=20006
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=44_L1_HCPCS_Code_20006 /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field
 Field ID=20007
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=44_L1_Modifier_1_20007 /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field
 Field ID=20008
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=44_L1_Modifier_2_20008 /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field
 Field ID=20009
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=45_L1_Service_Date_20009 /
boolean ID=LinkedCoord Value=true /
integer ID=LinkedFieldId Value=310 /
integer ID=LinkedLineId Value=0 /
   /AttrSet
 /Field
 Field ID=20010
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=44_L1_Rate_20010 /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field
 Field ID=20011
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=46_L1_Service_Units_20011 /
boolean ID=LinkedCoord Value=true /
integer ID=LinkedFieldId Value=315 /
integer ID=LinkedLineId Value=0 /
   /AttrSet
 /Field
 Field ID=20012
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=46_L1_Service_Units_Qualifier_20012 /
boolean ID=LinkedCoord Value=false /
integer ID=Type Value=3 /
integer ID=x Value=0 /
integer ID=y Value=0 /
integer ID=w Value=10 /
integer ID=h Value=10 /
   /AttrSet
 /Field
 Field ID=20013
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=47_L1_Total_Charges_20013 /
boolean ID=LinkedCoord Value=true /
integer ID=LinkedFieldId Value=320 /
integer ID=LinkedLineId Value=0 /
   /AttrSet
 /Field
 Field ID=20015
   AttrSet ID=Core
integer ID=ZoneId Value=999 /
string ID=Name Value=48_L1_Non_Covered_Charges_20015 /
boolean ID=LinkedCoord Value=true /
integer ID=LinkedFieldId Value=325 /
integer ID=LinkedLineId Value=0 /
   

Optimizing File handling operations

2009-11-05 Thread Parag Kalra
Hey Folks,

I Frequently use perl to process 2 files line by line.

Most of the times I compare two files line by line and check if one line is
same to corresponding line of other file, or if one line is substring of
other line etc and many more operations.

The technique which I generally use is to first save lines of two files in 2
different arrays.

Then execute a for loop where I compare corresponding index elements of the
2 arrays.

I guess this is not an optimize solution since if both files are huge
creating large sized arrays may consume lot of memory.

So wanted to know if there is any better approach to process 2 files line by
line.

Cheers,
Parag


Re: Optimizing File handling operations

2009-11-05 Thread John W. Krahn

Parag Kalra wrote:

Hey Folks,


Hello,


I Frequently use perl to process 2 files line by line.

Most of the times I compare two files line by line and check if one line is
same to corresponding line of other file, or if one line is substring of
other line etc and many more operations.

The technique which I generally use is to first save lines of two files in 2
different arrays.

Then execute a for loop where I compare corresponding index elements of the
2 arrays.

I guess this is not an optimize solution since if both files are huge
creating large sized arrays may consume lot of memory.

So wanted to know if there is any better approach to process 2 files line by
line.


You could do something like (UNTESTED):

while ( defined( my $first = FILE1 )  defined( my $second = FILE2 
) ) {




John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity.   -- Damian Conway

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




Re: Optimizing File handling operations

2009-11-05 Thread Jim Gibson

At 11:40 AM +0530 11/6/09, Parag Kalra wrote:

Hey Folks,

I Frequently use perl to process 2 files line by line.

Most of the times I compare two files line by line and check if one line is
same to corresponding line of other file, or if one line is substring of
other line etc and many more operations.

The technique which I generally use is to first save lines of two files in 2
different arrays.

Then execute a for loop where I compare corresponding index elements of the
2 arrays.

I guess this is not an optimize solution since if both files are huge
creating large sized arrays may consume lot of memory.

So wanted to know if there is any better approach to process 2 files line by
line.


Sure. Read both files one line at a time in a loop (untested):

open( my $fh1, ...
open( my $fh2, ...

for(;;) {
  my $line1 = $fh1;
  my $line2 = $fh2;
  last unless( defined $line1  defined $line2 );
  # compare lines
}

One potential problem is if the files do not have the same number of 
lines. You can check for $line1 or $line2 containing anything after 
the loop has terminated.




--
Jim Gibson
j...@gibson.org

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




Re: File Handling problem.

2008-03-28 Thread Ben Bullock
On Mar 28, 1:35 am, [EMAIL PROTECTED] (Ay) wrote:
 Hi,

 Current task requires me to combine a few files into a single file
 ( abc. txt ) where in each file has to be in a single page. I was able
 to create a combined file, but not able to ensure that each file
 resides in a page. Attempted a few things like 'format_lines_left'
 i.e $-.  in vain... One of the possibilities left with me is to give
 sufficient \n between each file that way ensuring that each file
 read in resides in a page (which is something that I would like to do
 at the last). Is there any other mechanism that can be done to ensure
 that each file resides in a page.. say page break character or
 something...

The traditional page break character for text files is the form-feed
character, which is
ascii 12, 0x0C, \f in Perl and C, or ^L if you use Emacs.

There's more information here:

http://en.wikipedia.org/wiki/Form_feed

and in Google.


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




File Handling problem.

2008-03-27 Thread AY
Hi,

Current task requires me to combine a few files into a single file
( abc. txt ) where in each file has to be in a single page. I was able
to create a combined file, but not able to ensure that each file
resides in a page. Attempted a few things like 'format_lines_left'
i.e $-.  in vain... One of the possibilities left with me is to give
sufficient \n between each file that way ensuring that each file
read in resides in a page (which is something that I would like to do
at the last). Is there any other mechanism that can be done to ensure
that each file resides in a page.. say page break character or
something...

Any help would be appreciated...


Thanks,
-Wg


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




Re: File Handling problem.

2008-03-27 Thread yitzle
Text files don't /have/ pages. The number of lines per page depends
on the printer driver - the font size, margin size, etc.
If you know the number of lines the print driver does per page, you
can fill to that point with newlines based on the number of lines
already outputted.
Or you might be interested in a PostScript/PDF module from CPAN...

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




Regarding file handling

2007-06-21 Thread Dharshana Eswaran

Hi All,

I am reading a certain data from one file and writing to another file. In
the original file, there are few lines, which occur more than once in
different lines. When i am writing it to the second file, i don't want it to
be written more than once. I mean, it should not be repetitive. The file are
just two simple text files.

In achieving what i need, i thought simultaneous reading and writing to the
file is required, to know if the lines are being written for the second
time. But i don't know how to achieve this.

A sample of the text file is shown below:
STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
{
   STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
protocol_discriminator;
   STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;
} STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;
};
STACK_CC_SS_COMMON_TYPE_CHANNEL_INFO_T
{
   STACK_CC_SS_COMMON_TYPE_CHANNEL_TYPE_T channel_type;
   STACK_CC_SS_COMMON_TYPE_CHANNEL_MODE_T channel_mode;
} STACK_CC_SS_COMMON_TYPE_CHANNEL_INFO_T;
};
STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
{
   STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
protocol_discriminator;
   STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;
} STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;
};

Can anyone guide me in this?

Thanks and Regards,
Dharshana


Re: Regarding file handling

2007-06-21 Thread Paul Lalli
On Jun 21, 9:31 am, [EMAIL PROTECTED] (Dharshana Eswaran) wrote:
 Hi All,

 I am reading a certain data from one file and writing to another file. In
 the original file, there are few lines, which occur more than once in
 different lines. When i am writing it to the second file, i don't want it to
 be written more than once. I mean, it should not be repetitive. The file are
 just two simple text files.

 In achieving what i need, i thought simultaneous reading and writing to the
 file is required, to know if the lines are being written for the second
 time. But i don't know how to achieve this.

 A sample of the text file is shown below:
 STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
 {
 STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
 protocol_discriminator;
 STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;} 
 STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;
 };

 STACK_CC_SS_COMMON_TYPE_CHANNEL_INFO_T
 {
 STACK_CC_SS_COMMON_TYPE_CHANNEL_TYPE_T channel_type;
 STACK_CC_SS_COMMON_TYPE_CHANNEL_MODE_T channel_mode;} 
 STACK_CC_SS_COMMON_TYPE_CHANNEL_INFO_T;
 };

 STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
 {
 STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
 protocol_discriminator;
 STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;

 } STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;
 };


It looks as though your data is delimited by };\n\n.  Is that
correct?  That is, each block of text ends with that string, and you
want to only print out the first instance of each block?  You can
control Perl's notion of what a line is via the $/ variable.  See
perldoc perlvar for more information.

To only print one instance of any given block, use a hash.  Each time
through your read loop, check to see if that block is in the hash.  If
it is, don't print it.  If it's not, do print it, and then add it to
the hash.  For example:

#!/usr/bin/perl
use strict;
use warnings;
open my $fh, '', infile.txt or die $!;
open my $ofh, '', outfile.txt or die $!;
local $/ = };\n\n;
my %printed;
while (my $record = $fh) {
   print $ofh $record unless $printed{$record}++;
}
__END__

Hope that helps,
Paul Lalli


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




Re: Regarding file handling

2007-06-21 Thread Dharshana Eswaran

On 6/21/07, Paul Lalli [EMAIL PROTECTED] wrote:


On Jun 21, 9:31 am, [EMAIL PROTECTED] (Dharshana Eswaran) wrote:
 Hi All,

 I am reading a certain data from one file and writing to another file.
In
 the original file, there are few lines, which occur more than once in
 different lines. When i am writing it to the second file, i don't want
it to
 be written more than once. I mean, it should not be repetitive. The file
are
 just two simple text files.

 In achieving what i need, i thought simultaneous reading and writing to
the
 file is required, to know if the lines are being written for the second
 time. But i don't know how to achieve this.

 A sample of the text file is shown below:
 STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
 {
 STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
 protocol_discriminator;
 STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T
transaction_id;} STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;
 };

 STACK_CC_SS_COMMON_TYPE_CHANNEL_INFO_T
 {
 STACK_CC_SS_COMMON_TYPE_CHANNEL_TYPE_T channel_type;
 STACK_CC_SS_COMMON_TYPE_CHANNEL_MODE_T channel_mode;}
STACK_CC_SS_COMMON_TYPE_CHANNEL_INFO_T;
 };

 STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
 {
 STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
 protocol_discriminator;
 STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;

 } STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;
 };


It looks as though your data is delimited by };\n\n.  Is that
correct?  That is, each block of text ends with that string, and you
want to only print out the first instance of each block?  You can
control Perl's notion of what a line is via the $/ variable.  See
perldoc perlvar for more information.

To only print one instance of any given block, use a hash.  Each time
through your read loop, check to see if that block is in the hash.  If
it is, don't print it.  If it's not, do print it, and then add it to
the hash.  For example:

#!/usr/bin/perl
use strict;
use warnings;
open my $fh, '', infile.txt or die $!;
open my $ofh, '', outfile.txt or die $!;
local $/ = };\n\n;
my %printed;
while (my $record = $fh) {
   print $ofh $record unless $printed{$record}++;
}
__END__

Hope that helps,
Paul Lalli


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




I am unable to get the desired result. Its printing all the instances of the
block.

Thanks and Regards,
Dharshana


Re: Regarding file handling

2007-06-21 Thread Tom Phoenix

On 6/21/07, Dharshana Eswaran [EMAIL PROTECTED] wrote:


I am unable to get the desired result. Its printing all the instances of the
block.


Please post the smallest self-contained example program which other
people can use to see what you're doing, with what data. Ideally,
narrow things down to the one line of code or piece of data that does
something, or fails to do something, as appropriate. (For example, if
you're using the algorithm that Paul Lalli supplied, could two or more
very similar strings cause you mistakenly to think you're seeing
duplicate output?)

Good luck with it!

--Tom Phoenix
Stonehenge Perl Training

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




Re: Regarding file handling

2007-06-21 Thread Dharshana Eswaran

Actually my work is from the text file, i need to consider each entry in
every structure, and find its data type, to which its typedefed to and then
assign the values according to the value of the datatype. For eg:

A sample of the text file is shown below:
STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
{
   STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
protocol_discriminator;
   STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;
} STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;
};
STACK_CC_SS_COMMON_TYPE_CHANNEL_INFO_T
{
   STACK_CC_SS_COMMON_TYPE_CHANNEL_TYPE_T channel_type;
   STACK_CC_SS_COMMON_TYPE_CHANNEL_MODE_T channel_mode;
} STACK_CC_SS_COMMON_TYPE_CHANNEL_INFO_T;
};
STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
{
   STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
protocol_discriminator;
   STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;
} STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;
};



From the above text, i need to read

STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
and find to which data type this is type defed to and then asign the values
according to the value of the data type.

Here the basic data types are UINT8 and UINT16 which would read one and two
bytes respectively. All the other data types are type  defed to one of the
two.

My code in generating the file is as shown: this is just a module from the
original code

#!/usr/bin/perl
use strict;
use warnings;
$file = filename;
unless (open(INNER, $file)) {
   die(Cannot open file \n);
   }

unless (open(INN1, +outfile.txt)) {
   die(Cannot open file \n);
   }

   while ($innergrep = INNER) {
   chop($innergrep);
   while((($inn = index ($innergrep, typedef struct)) = 0)||(($inn =
index ($innergrep, typedef union)) = 0)) {
   $p = 0;
   while($inn = (index ($innergrep, }))  0) {
   $innergrep = INNER;
   chop($innergrep);
   $innerstore[$p] = $innergrep;
   $p++;
   }

   if($inn = (index ($innergrep, $innerB;)) = 0) {

   $innerlen = @innerstore;
   print INN1 $innerB\n;
   for(my $j=0; $j$p; $j++) {
   print INN1 $innerstore[$j]\n;

   }
   print INN1 };\n;


   }
   }
   }

The above code is just a small module from the original code. In the above
code, the file name is passed as an argument and it is stored in $file. From
the file which is specified, i need to search for the structure associated
with the primitive supplied in $innerB. As in the file the structure is
written as

STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
{
   STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
protocol_discriminator;
   STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;
} STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;

The primitive name is found only at the end of the structure. So initially
read the contents, store it in a array and and then when the primitive name
in the structure matched with the primitive name passed here, then i write
it to the outfile.txt.
As, each time the primitive is passed through some variable, i am unable to
check the duplicate copies. That is, if the same primitive is passed again
and again, my code just gets the structure and stores it multiple times. I
want it to store only once. That is the problem.

I hope this describes the problem to all. I kindly request you to guide me
in this.

Thanks and Regards,
Dharshana


On 6/22/07, Tom Phoenix [EMAIL PROTECTED] wrote:


On 6/21/07, Dharshana Eswaran [EMAIL PROTECTED] wrote:

 I am unable to get the desired result. Its printing all the instances of
the
 block.

Please post the smallest self-contained example program which other
people can use to see what you're doing, with what data. Ideally,
narrow things down to the one line of code or piece of data that does
something, or fails to do something, as appropriate. (For example, if
you're using the algorithm that Paul Lalli supplied, could two or more
very similar strings cause you mistakenly to think you're seeing
duplicate output?)

Good luck with it!

--Tom Phoenix
Stonehenge Perl Training



Re: Regarding file handling

2007-06-21 Thread Prabu Ayyappan
Hope this helps you.

use strict;
use warnings;
open my $fh, '', infile.txt or die $!;
open my $ofh, '', outfile.txt or die $!;
local $/ = };;
my %printed;
my @a  = $fh;
my @b = ();
foreach(@a){
if ($_ =~ m/(.*?)\_T\n/){
my $tomatch = $1;
my $cnt =  grep /$tomatch/,@b;
if($cnt == 0){
push(@b,$_);
}
#print $tomatch.\n;
}
}
print $ofh @b;

Thanks and Regards,
Prabu.M.A

Dharshana Eswaran [EMAIL PROTECTED] wrote: Hi All,

I am reading a certain data from one file and writing to another file. In
the original file, there are few lines, which occur more than once in
different lines. When i am writing it to the second file, i don't want it to
be written more than once. I mean, it should not be repetitive. The file are
just two simple text files.

In achieving what i need, i thought simultaneous reading and writing to the
file is required, to know if the lines are being written for the second
time. But i don't know how to achieve this.

A sample of the text file is shown below:
STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
{
STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
protocol_discriminator;
STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;
} STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;
};
STACK_CC_SS_COMMON_TYPE_CHANNEL_INFO_T
{
STACK_CC_SS_COMMON_TYPE_CHANNEL_TYPE_T channel_type;
STACK_CC_SS_COMMON_TYPE_CHANNEL_MODE_T channel_mode;
} STACK_CC_SS_COMMON_TYPE_CHANNEL_INFO_T;
};
STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
{
STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
protocol_discriminator;
STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;
} STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;
};

Can anyone guide me in this?

Thanks and Regards,
Dharshana


   
-
Get the free Yahoo! toolbar and rest assured with the added security of spyware 
protection. 

Re: Regarding file handling

2007-06-21 Thread Dharshana Eswaran

My outfile should look like this:

STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T = UINT8,
   STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T = UINT8,
   STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T = UINT8,
   STACK_CC_SS_COMMON_TYPE_CHANNEL_TYPE_T = UINT8,
   STACK_CC_SS_COMMON_TYPE_CHANNEL_MODE_T = UINT8,
   STACK_CC_SS_COMMON_TYPE_CHANNEL_MODE_T = UINT8,
   STACK_REG_COMMON_TYPE_RAB_ID_T = UINT8,

But without repetitions


Thanks and Regards,
Dharshana

On 6/22/07, Dharshana Eswaran [EMAIL PROTECTED] wrote:


Actually my work is from the text file, i need to consider each entry in
every structure, and find its data type, to which its typedefed to and then
assign the values according to the value of the datatype. For eg:

A sample of the text file is shown below:
STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
{
STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
protocol_discriminator;
STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;
} STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;
};
STACK_CC_SS_COMMON_TYPE_CHANNEL_INFO_T
{
STACK_CC_SS_COMMON_TYPE_CHANNEL_TYPE_T channel_type;
STACK_CC_SS_COMMON_TYPE_CHANNEL_MODE_T channel_mode;
} STACK_CC_SS_COMMON_TYPE_CHANNEL_INFO_T;
};
STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
{
STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
protocol_discriminator;
STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;
} STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;
};


From the above text, i need to read 
STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
and find to which data type this is type defed to and then asign the values
according to the value of the data type.

Here the basic data types are UINT8 and UINT16 which would read one and
two bytes respectively. All the other data types are type  defed to one of
the two.

My code in generating the file is as shown: this is just a module from the
original code

#!/usr/bin/perl
use strict;
use warnings;
$file = filename;
unless (open(INNER, $file)) {
die(Cannot open file \n);
}

unless (open(INN1, +outfile.txt)) {
die(Cannot open file \n);
}

while ($innergrep = INNER) {
chop($innergrep);
while((($inn = index ($innergrep, typedef struct)) = 0)||(($inn
= index ($innergrep, typedef union)) = 0)) {
$p = 0;
while($inn = (index ($innergrep, }))  0) {
$innergrep = INNER;
chop($innergrep);
$innerstore[$p] = $innergrep;
$p++;
}

if($inn = (index ($innergrep, $innerB;)) = 0) {

$innerlen = @innerstore;
print INN1 $innerB\n;
for(my $j=0; $j$p; $j++) {
print INN1 $innerstore[$j]\n;

}
print INN1 };\n;


}
}
}

The above code is just a small module from the original code. In the above
code, the file name is passed as an argument and it is stored in $file. From
the file which is specified, i need to search for the structure associated
with the primitive supplied in $innerB. As in the file the structure is
written as

STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T
{
STACK_CC_SS_COMMON_TYPE_REFERENCE_PROTOCOL_DIS_T
protocol_discriminator;
STACK_CC_SS_COMMON_TYPE_REFERENCE_TRANSACTION_ID_T   transaction_id;
} STACK_CC_SS_COMMON_TYPE_REFERENCE_ID_T;

The primitive name is found only at the end of the structure. So initially
read the contents, store it in a array and and then when the primitive name
in the structure matched with the primitive name passed here, then i write
it to the outfile.txt.
As, each time the primitive is passed through some variable, i am unable
to check the duplicate copies. That is, if the same primitive is passed
again and again, my code just gets the structure and stores it multiple
times. I want it to store only once. That is the problem.

I hope this describes the problem to all. I kindly request you to guide me
in this.

Thanks and Regards,
Dharshana


On 6/22/07, Tom Phoenix [EMAIL PROTECTED] wrote:

 On 6/21/07, Dharshana Eswaran [EMAIL PROTECTED] wrote:

  I am unable to get the desired result. Its printing all the instances
 of the
  block.

 Please post the smallest self-contained example program which other
 people can use to see what you're doing, with what data. Ideally,
 narrow things down to the one line of code or piece of data that does
 something, or fails to do something, as appropriate. (For example, if
 you're using the algorithm that Paul Lalli supplied, could two or more
 very similar strings cause you mistakenly to think you're seeing
 duplicate output?)

 Good luck with it!

 --Tom Phoenix
 Stonehenge Perl Training





Re: File Handling. Reading and Writting.

2007-05-15 Thread Ken Foskey

 It looks as if you're trying to edit a text file in place. Although
 that's possible for some simple cases, it's generally easier to use
 Perl's $^I functionality.
 

What is $^I?


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




Re: File Handling. Reading and Writting.

2007-05-15 Thread Chas Owens

On 5/15/07, Ken Foskey [EMAIL PROTECTED] wrote:


 It looks as if you're trying to edit a text file in place. Although
 that's possible for some simple cases, it's generally easier to use
 Perl's $^I functionality.


What is $^I?


It is a special scalar variable that turns on/off in-place editing.

from perldoc perlvar
  $^I The current value of the inplace-edit extension.  Use undef
  to disable inplace editing.  (Mnemonic: value of -i switch.)

from perldoc perlrun
  -i[extension]
   specifies that files processed by the  construct are to be
   edited in-place.  It does this by renaming the input file, opening
   the output file by the original name, and selecting that output
   file as the default for print() statements.

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




Re: File Handling. Reading and Writting.

2007-05-15 Thread Bruno Schroeder
Hello Tom!
Hello All!

You helped a lot Tom. Still... I have some trouble.

For sure $^I is easier to use. Although in my case i would need to rewrite a 
lot of code. I will do it, but it will take a while to test it. I found good 
information on $^I at 
http://perldoc.perl.org/perlfaq5.html#How-can-I-use-Perl%27s-%27-i%27-option-from-within-a-program%3F

I found some information for seek() at 
http://perldoc.perl.org/functions/seek.html

I tryed to use seek but it did not work. Can you help me on that, please? I 
am using Windows XP. The following example writes at the end of the file.

use strict;
my $file = teste_rw.txt;
open (FILE, +, $file) or die Can not open $file: $!.;
for my $line (FILE) {
print $line;
seek(FILE, 0, 1);
print FILE b\n;
seek(FILE, 0, 1);
}
my $a_while = 2;
sleep($a_while);
seek(FILE, tell(FILE), 0);
close FILE;

Thank you.
Bruno


Tom Phoenix [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 On 5/14/07, Bruno Schroeder [EMAIL PROTECTED] wrote:

 I am trying to read and write in a file, I am using something like:

 open (FILE, +teste_rw.txt) or die I couldn't open the file.;

 A little better is to include $! in the message, maybe like this:

  open FILE, +teste_rw.txt
or die Can't open r/w 'teste_rw.txt': $!;

 for my $line (FILE) {
  print $line;
  if($line eq X\n)
  {
 print FILE b\n

 Depending upon your I/O system, you may need to use seek() whenever
 you switch from reading to writing, or from writing to reading.

 It looks as if you're trying to edit a text file in place. Although
 that's possible for some simple cases, it's generally easier to use
 Perl's $^I functionality.

 Hope this helps!

 --Tom Phoenix
 Stonehenge Perl Training 



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




Re: File Handling. Reading and Writting.

2007-05-15 Thread Tom Phoenix

On 5/15/07, Bruno Schroeder [EMAIL PROTECTED] wrote:


I tryed to use seek but it did not work. Can you help me on that, please? I
am using Windows XP. The following example writes at the end of the file.

use strict;
my $file = teste_rw.txt;
open (FILE, +, $file) or die Can not open $file: $!.;
for my $line (FILE) {
print $line;
seek(FILE, 0, 1);
print FILE b\n;
seek(FILE, 0, 1);
}
my $a_while = 2;
sleep($a_while);
seek(FILE, tell(FILE), 0);
close FILE;


Here's some untested code that may do something resembling what you want:

 my $file = teste_rw.txt;
 open (FILE, +, $file) or die Can not open '$file' r/w: $!;

 # $next_loc is the location of the next line to process
 my $next_loc = tell FILE;  # probably 0

 while (1) {

   my $current_loc = $next_loc;
   # seek before each read or write
   seek(FILE, $current_loc, 0) or die;
   my $line = FILE;
   last if not defined $line;  # undef at eof
   $next_loc = tell FILE;

   print $line;

   # Get the replacement string (somehow).
   my $repl = replacement_for($line);

   die Can't replace '$line' with '$repl'
 unless length($line) == length($repl);

   # seek before each read or write
   seek(FILE, $current_loc, 0) or die;
   print FILE $repl;
 }

 close FILE;

I'm not sure why your code used sleep, so I omitted it. Cheers!

--Tom Phoenix
Stonehenge Perl Training

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




Re: File Handling. Reading and Writting.

2007-05-15 Thread Bruno Schroeder
Thank you Tom!
We realy have lots of ways to do everything. In this case, is easier to use 
$^I functionality, and I did. Although, it is very nice to see this code 
bellow.

Cheers!

Tom Phoenix [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 On 5/15/07, Bruno Schroeder [EMAIL PROTECTED] wrote:

 I tryed to use seek but it did not work. Can you help me on that, please? 
 I
 am using Windows XP. The following example writes at the end of the file.

 use strict;
 my $file = teste_rw.txt;
 open (FILE, +, $file) or die Can not open $file: $!.;
 for my $line (FILE) {
 print $line;
 seek(FILE, 0, 1);
 print FILE b\n;
 seek(FILE, 0, 1);
 }
 my $a_while = 2;
 sleep($a_while);
 seek(FILE, tell(FILE), 0);
 close FILE;

 Here's some untested code that may do something resembling what you want:

  my $file = teste_rw.txt;
  open (FILE, +, $file) or die Can not open '$file' r/w: $!;

  # $next_loc is the location of the next line to process
  my $next_loc = tell FILE;  # probably 0

  while (1) {

my $current_loc = $next_loc;
# seek before each read or write
seek(FILE, $current_loc, 0) or die;
my $line = FILE;
last if not defined $line;  # undef at eof
$next_loc = tell FILE;

print $line;

# Get the replacement string (somehow).
my $repl = replacement_for($line);

die Can't replace '$line' with '$repl'
  unless length($line) == length($repl);

# seek before each read or write
seek(FILE, $current_loc, 0) or die;
print FILE $repl;
  }

  close FILE;

 I'm not sure why your code used sleep, so I omitted it. Cheers!

 --Tom Phoenix
 Stonehenge Perl Training 



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




File Handling. Reading and Writting.

2007-05-14 Thread Bruno Schroeder
Hello All.

I am trying to read and write in a file, I am using something like:

open (FILE, +teste_rw.txt) or die I couldn't open the file.;
for my $line (FILE) {
 print $line;
 if($line eq X\n)
 {
print FILE b\n
 } else
 {
print FILE c\n
 }
}
print FILE Y\n;
close FILE;

In the attached file, I whant to write inside the $modify_class, 
$modify_method and $modify_parameter tests.

   Thnak you.
Bruno


begin 666 checkTracev10.pl
M=7-E('-TFEC=#L-@T*(V-H96-K5')A8V4NP-B-,:7-T(')E8W5RVEV
M96QY(EN(5V97)Y('-U8F9O;1EB!O9B!A(EN:[EMAIL PROTECTED]ER96-T;W)Y
M+!A;[EMAIL PROTECTED]')A8V4M:[EMAIL 
PROTECTED]('1R86-E+6]U=!E'!R97-S:6]NR!T
M:%T(%R92!N;[EMAIL PROTECTED];W)D:6YG('1O('1H92!M971H;V0G[EMAIL PROTECTED]
M#0HC1VQA8F%L(%9AFEA8FQESH-FUY(1F:6QE7V-O=6YT(#T@,#L-FUY
M(1P86-K86=E(#T@(CQP86-K86=E/B([#0IM2 D8VQAW,@/2 B/-L87-S
M/B([#0IM2 D;65T:]D(#T@(CQM971H;V0^(CL-FUY($!P87)A;65T97)S
M(#T@(B([#0IM2 D8VQAW-?;6ES=%K95]C;W5N= ](# [#0IM2 D;65T
M:]D7VUIW1A:V5?8V]U;G0@/2 P.PT*;7D@)'!AF%M971EE]M:7-T86ME
M7V-O=6YT(#T@,#L-FUY(1V97)I9GE?8VQAW,@/2 B=')U92([#0IM2 D
M=F5R:69Y7VUE=AO9 ]()TG5E(CL-FUY(1V97)I9GE?%R86UE=5R
M(#T@(G1R=64B.PT*;7D@)UO9EF5]C;%SR ]()TG5E(CL-FUY(1M
M;V1I9GE?;65T:]D(#T@(G1R=64B.PT*;7D@)UO9EF5]P87)A;65T97(@
M/2 B=')U92([#0IM2 D=5M ](# [#0H-B-';[EMAIL PROTECTED]W1A;G1S
[EMAIL PROTECTED];7D@)EN:71I86Q?9ER96-T;W)Y(#T@(D,Z7%QBG%7'-R8UQ0E)6
M4([#0IM2 D9FEL95]E'!R97-S:6]N(#T@(BYJ879A(CL-FUY(1P86-K
M86=E7V5X')EW-I;VX@/2 B%C:V%G92!;7CM=*SLB.PT*;7D@)-L87-S
M7V5X')EW-I;VX@/2 B7G!U8FQI8R!C;%SR([#0HC;7D@)UE=AO9%]E
M'!R97-S:6]N(#T@(G!U8FQI8UQS*UQW*UQS*UQW*EPH(CL-FUY(1L;V=?
M95B=6=?97APF5SVEO;B ](),;V=-86YA9V5R7XH87!P1$%/3]G9V5R
M?%P$-O;G1R;VQL97),;V=G97(I7YD96)U9R([#0HC;7D@)'1R86-E7V5X
M')EW-I;VX@/2 B(CL-GL-@EPFEN=@B8VAE8VM4F%C92YP;%QN0V]N
MW1A;G1S.EQN(BD[#0H-@EPFEN=@)(FEN:71I86Q?9ER96-T;W)Y(#T@
M(BP@)EN:71I86Q?9ER96-T;W)Y+ B7XB+ T*0D)(F9I;5?97APF5S
MVEO;B ]((L(1F:6QE7V5X')EW-I;VXL();B(L#0H)0DB%C:V%G
M95]E'!R97-S:6]N(#T@(BP@)'!A8VMA9V5?97APF5SVEO;BP@(EQN(BP-
M@D)2)C;%SU]E'!R97-S:6]N(#T@(BP@)-L87-S7V5X')EW-I;VXL
M();B(L#0H)0DC(FUE=AO9%]E'!R97-S:6]N(#T@(BP@)UE=AO9%]E
M'!R97-S:6]N+ B7XB+ T*0D)(FQO9U]D96)U9U]E'!R97-S:6]N(#T@
M(BP@)QO9U]D96)U9U]E'!R97-S:6]N+ B7XB+ T*0D)(R)TF%C95]E
M'!R97-S:6]N(#T@(BP@)'1R86-E7V5X')EW-I;VXL();B(L#0H)0DB
M=F5R:69Y7V-L87-S(#T@(BP@)'9EFEF5]C;%SRP@(EQN(BP-@D)2)V
M97)I9GE?;65T:]D(#T@(BP@)'9EFEF5]M971H;V0L();B(L#0H)0DB
M=F5R:69Y7W!AF%M971EB ]((L(1V97)I9GE?%R86UE=5R+ B7XB
M+ T*0D)(FUO9EF5]C;%SR ]((L(1M;V1I9GE?8VQAW,L();B(L
M#0H)0DB;6]D:69Y7VUE=AO9 ]((L(1M;V1I9GE?;65T:]D+ B7XB
M+ T*0D)(FUO9EF5]P87)A;65T97(@/2 B+ D;6]D:69Y7W!AF%M971E
MBP@(EQN(BP-@D)2);B(L*3L-@T*6QIW1?F5C=7)C:79E;'DH)EN
M:71I86Q?9ER96-T;W)Y*3L-@EPFEN=@B1F]U;F0@(BP@)-L87-S7VUI
MW1A:V5?8V]U;G0L((@;6ES=%K97,@;[EMAIL PROTECTED]W,@;F%M92P@(BP@)UE
M=AO9%]M:7-T86ME7V-O=6YT+ B(UIW1A:V5S(]N(UE=AO9!M86UE
M+ B+ D%R86UE=5R7VUIW1A:V5?8V]U;G0L((@;6ES=%K97,@;VX@
M%R86UE=5R(YA;64N7XB*3L-@EPFEN=@B1F]U;F0@(BP@)-L87-S
M7VUIW1A:V5?8V]U;[EMAIL PROTECTED] D;65T:]D7VUIW1A:V5?8V]U;[EMAIL PROTECTED] 
D%R
M86UE=5R7VUIW1A:V5?8V]U;G0L((@;6ES=%K97,N7XB*3L-@EPFEN
M=@B5F5R:69I960@(BP@)9I;5?8V]U;G0L((@(BP@)9I;5?97APF5S
MVEO;BP@(B!F:6QERY;B(I.PT*?0T*#0IS=6(@;ES=%]R96-UF-I=F5L
M0T*PT*6]P96YD:7(H9ER96-T;W)Y+ D7ULP72D[#0H@( @;[EMAIL PROTECTED]1I
MF5C=]R5]F:6QER ](')E861D:7(H9ER96-T;W)Y*3L-B @(!F;W)E
M86-H(UY(1D:7)E8W1OGE?9FEL97,@*$!D:7)E8W1OGE?9FEL97,I('L-
MB @( @( @;7D@)9I;5N86UE(#T@)%];,[EMAIL PROTECTED] B7%PB(X@)1IF5C
M=]R5]F:6QESL-B @( @( @:[EMAIL PROTECTED]$H)1IF5C=]R5]F:6QER!E
M2 G+B@?'P@)1IF5C=]R5]F:6QER!E2 G+BXG*2D-@D)PT*0D)
M:[EMAIL PROTECTED]UD(1F:6QE;F%M92D-@D)7L-@D)0EL:7-T7W)E8W5R8VEV96QY
M*1F:6QE;F%M92D[#0H)0E](5L[EMAIL PROTECTED])0E[#0H)0D)=')E871?9FEL
[EMAIL PROTECTED];64I.PT*0D)?0T*0E]#0H@(!]#0H@( @8VQOV5D:7(H
M9ER96-T;W)Y*3L-GT-@T*W5B('1R96%T7V9I;4-GL-@EM2 D9FEL
M96YA;64@/2 D7ULP73L-@EI9B HW5BW1R*1F:6QE;F%M92P@;5N9W1H
M*1F:6QE;[EMAIL PROTECTED]@H)9I;5?97APF5SVEO;BDL(QE;F=T
M:@D9FEL96YA;64I*2!E2 D9FEL95]E'!R97-S:6]N*0T*7L-@D);W!E
M;BAF:6QE+ B*SPB+ D9FEL96YA;64I(]R('!R:6YT*)#86X@;F]T(]P
M96X@(B K(1F:6QE;F%M92 K();B(I.PT*0DD9FEL95]C;W5N=LK.PT*
M0EM2 D;EN95]N=6UB97(@/2 P.PT*0EF;W(@;7D@)QI;[EMAIL PROTECTED]:6QE
M/[EMAIL PROTECTED])7L-@D)21L:6YE7VYU;6)EBLK.PT*0D):68H)QI;F4@/7X@
M+R1P86-K86=E7V5X')EW-I;VXO*0T*0D)PT*0D)6UY($!T96UP(#T@
MW!L:70H+UQS*WP[+RP@)QI;F4I.PT*0D)21P86-K86=E([EMAIL PROTECTED]'1E;7!;
M,5T[#0H)0D)(W!R:6YT*)P86-K86=E(#T@(BP@)'!A8VMA9V4L();B(I
M.PT*0D)?0T*0D):68H)QI;F4@/[EMAIL PROTECTED];%SU]E'!R97-S:6]N+RD-
M@D)7L-@D)0EM2! =5M ]('-P;ET*]RLO+ D;EN92D[#0H)
M0D))-L87-S([EMAIL PROTECTED]'1E;7!;,ET[#0H)0D)(W!R:6YT*)C;%SR ]((L
M(1C;%SRP@(EQN(BD[#0H)0E]#0H)0EM2! W!L:71E9%]L:6YE(#T@
MW!L:70H+UQS*WQ*'Q*2\L(1L:6YE*3L-@D)6EF*1SQI=5D7VQI
M;F5;,[EMAIL PROTECTED]@(G!U8FQI8R(@)B DW!L:71E9%]L:6YE6R0CW!L:71E9%]L
M:6YE72!E2 B7'LB*0T*0D)PT*0D)21M971H;V0@/2 DW!L:71E9%]L
M:6YE6S-=.PT*0D)6UY(1P87)A;65T97)?:6YD97@@/2 Q.PT*0D)6UY
M(1I([EMAIL PROTECTED]@D)0EW:[EMAIL 
PROTECTED]1SQI=5D7VQI;F5;)E=(5Q()T
M:')O=W,B('P@)'-P;ET961?;EN95LD:[EMAIL 

Re: File Handling. Reading and Writting.

2007-05-14 Thread Tom Phoenix

On 5/14/07, Bruno Schroeder [EMAIL PROTECTED] wrote:


I am trying to read and write in a file, I am using something like:

open (FILE, +teste_rw.txt) or die I couldn't open the file.;


A little better is to include $! in the message, maybe like this:

 open FILE, +teste_rw.txt
   or die Can't open r/w 'teste_rw.txt': $!;


for my $line (FILE) {
 print $line;
 if($line eq X\n)
 {
print FILE b\n


Depending upon your I/O system, you may need to use seek() whenever
you switch from reading to writing, or from writing to reading.

It looks as if you're trying to edit a text file in place. Although
that's possible for some simple cases, it's generally easier to use
Perl's $^I functionality.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

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




Re: File handling using CGI

2006-07-27 Thread Prabu

I BioKid wrote:

One simple question -
I need to accept a file from the user and to store it as temp1.
then I need to give this file as an input of another program :
I wrote a script like this, but it is not working : Help ?

#!/usr/bin/perl -w
use CGI;
my $q = new CGI;
my $file = $q-param('file');
print $q-header();

# Reading file from user and writing to another file temp1
open (FOO, temp1);
while ($file)
   {
   print FOO $_;
   }
close FOO;

# internaly executing xxx program; taking temp1 as input
`/usr/bin/xxx temp1`;

#temp.xxx is output of usr/bin/xxx
@psa = `cat temp1.xxx`;

foreach $i(@psa)
{
   print $i;
   print br;
}


Can u explain whats your requirement little more clearly.

You mean reading the file name from the user and copying the file to 
temp1 and then using this temp1.


then you can use the

`cp file1 temp1`

and then to use it in another file.

If not please explain it little more :(

--
Prabu.M.A
When I was born I was so surprised
I didnt talk for a period and half
 -Gracie Allen


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




Re: File handling using CGI

2006-07-27 Thread I BioKid

Dear Prabu and all,

My purpose is simple,
I have a web-form, that will accept a file,
I need to use this file as an input of a program in my server,
then I need to print the out put on the web page.

I am able to do the second part, but am not able to get the users file to a
variable or array ?
I just want to know how to pass the users file to a file called temp in my
server.

I tried this many time :( - pls help
--
thanks in advance !!!


RE: File handling using CGI

2006-07-27 Thread Charles K. Clarkson
I BioKid wrote:

: I am able to do the second part, but am not able to get
: the users file to a variable or array ?

Read the Files and I/O section of the perlintro
file in the perl documentation.


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
Free Market Advocate
Web Programmer

254 968-8328


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




Re: File handling using CGI

2006-07-27 Thread Ken Foskey
On Wed, 2006-07-26 at 23:57 +0530, I BioKid wrote:
 One simple question -
 I need to accept a file from the user and to store it as temp1.
 then I need to give this file as an input of another program :
 I wrote a script like this, but it is not working : Help ?
 
 #!/usr/bin/perl -w
 use CGI;
 my $q = new CGI;
 my $file = $q-param('file');
 print $q-header();
 
 # Reading file from user and writing to another file temp1
 open (FOO, temp1);
 while ($file)
 {
 print FOO $_;
 }
 close FOO;
 
 # internaly executing xxx program; taking temp1 as input
 `/usr/bin/xxx temp1`;
 
 #temp.xxx is output of usr/bin/xxx
 @psa = `cat temp1.xxx`;
 
 foreach $i(@psa)
 {
 print $i;
 print br;
 }


I think that you may want to look up pipes.  You can pass in data to a
program, have it process it and then get the output out from that
program without using an intermediate file.

I am curious why you wrote this:

 @psa = `cat temp1.xxx`;

foreach $i(@psa)

When you had this a little earlier in your code.

open (FOO, temp1);
while ($file)



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




File handling using CGI

2006-07-26 Thread I BioKid

One simple question -
I need to accept a file from the user and to store it as temp1.
then I need to give this file as an input of another program :
I wrote a script like this, but it is not working : Help ?

#!/usr/bin/perl -w
use CGI;
my $q = new CGI;
my $file = $q-param('file');
print $q-header();

# Reading file from user and writing to another file temp1
open (FOO, temp1);
while ($file)
   {
   print FOO $_;
   }
close FOO;

# internaly executing xxx program; taking temp1 as input
`/usr/bin/xxx temp1`;

#temp.xxx is output of usr/bin/xxx
@psa = `cat temp1.xxx`;

foreach $i(@psa)
{
   print $i;
   print br;
}

--
thanks in advance,
IBK


File Handling

2004-04-05 Thread senthil prabu
   Hi,
I have wrote a script to search for a pattern and replace it in all files of a 
directory,that i specified at commandline.I want another one thing is to be done in 
the script.That's,it should search only for the type of files I specified at 
commandline.That is,it should get the extension of file name and search only those 
files in the directory.
 
   I have made a attempt in this script,but it goes on asking extension for all the 
files in directory.
   Plz help me in getting the things right in the following script.
   Tell the changes to be made in this.
 
#! /usr/bin/perl 

print Enter a path name: ;
my $path=STDIN;
chomp($path);
opendir THISDIR, $path or die serious dainbramage: $!;
my @allfiles = readdir THISDIR;
 
# get all files
foreach $file (@allfiles){
$filetoopen = $path ./ .$file;
 
# filter to check the type of file
print Enter the type of extension:;
my $ext=STDIN;
chomp($ext);
($str1,$str2) = split(/./, $filetoopen);
if($str2 eq $ext)
{
print $str2;
$filetoopen1 = join(.,$str1,$str2);
print $filetoopen1;
open(IN, $filetoopen1) || die cannot open file\n;
open(OUT, $test) || die cannot open file\n;
while (IN){
if (/$com/){
s/$com/td\n/g\\script/g;
}
if (/$img/){
s/$img/\nscript\nimg/g;
}
if (/$pattern/){
s/$pattern/$own/g;
#   print $_;
}
if (/img/){
s/$img/document.write(img/g;
}
if (/$com/){
s/$com/td);/g;
}
print OUT $_;
}
close (OUT);
close (IN);
rename($test,$filetoopen1);
}
}
__END__

 
 Thanks inadvance,
 Prabu.


Win an evening with the Indian cricket captain: Yahoo! India Promos.

RE: File Handling

2004-04-05 Thread Traeder, Philipp
Hi Prabu,

 I have wrote a script to search for a pattern and replace 
 it in all files of a directory,that i specified at 
 commandline.I want another one thing is to be done in the 
 script.That's,it should search only for the type of files I 
 specified at commandline.That is,it should get the extension 
 of file name and search only those files in the directory.

I have made a attempt in this script,but it goes on asking 
 extension for all the files in directory.
Plz help me in getting the things right in the following script.
Tell the changes to be made in this.
  
 #! /usr/bin/perl 
 
 print Enter a path name: ;
 my $path=STDIN;
 chomp($path);
 opendir THISDIR, $path or die serious dainbramage: $!;
 my @allfiles = readdir THISDIR;
  
 # get all files
 foreach $file (@allfiles){
 $filetoopen = $path ./ .$file;
  

The following lines are the problem:

 # filter to check the type of file
 print Enter the type of extension:;
 my $ext=STDIN;
 chomp($ext);

You're asking for the extension inside the foreach-loop, therefore your
application
asks you for the extension everytime it processes a file. Simply move those
lines up
(in front of the get all files comment), and everything should work. :-)

 ($str1,$str2) = split(/./, $filetoopen);
 if($str2 eq $ext)
 {
 print $str2;
 $filetoopen1 = join(.,$str1,$str2);
 print $filetoopen1;
 open(IN, $filetoopen1) || die cannot open file\n;
 open(OUT, $test) || die cannot open file\n;
 while (IN){
 if (/$com/){
 s/$com/td\n/g\\script/g;
 }
 if (/$img/){
 s/$img/\nscript\nimg/g;
 }
 if (/$pattern/){
 s/$pattern/$own/g;
 #   print $_;
 }
 if (/img/){
 s/$img/document.write(img/g;
 }
 if (/$com/){
 s/$com/td);/g;
 }
 print OUT $_;
 }
 close (OUT);
 close (IN);
 rename($test,$filetoopen1);
 }
 }
 __END__
 
  

The problem you're trying to solve - replacing one pattern with another in
multiple
files - is actually a very common one for perl scripts. Thus there´s some
built-in
perl magic for handling scenarios like this:

A) working on multiple files line per line

Take the following code and save it in a perl script called
multiplefiles.pl:

# START multiplefiles.pl #
#! /usr/bin/perl -w

use strict;

while () {
print processing line ($_) in file $ARGV\n;
}
# END multiplefiles.pl #

Now call it on command line like this:
multiplefiles.pl myfile1.txt myfile2.txt

As you hopefully see, this code opens every file and processes it line by
line...

B) Globbing

Adding one line, perl lets you specify wildcards on the command line:

# STARTUP multiplefiles2.pl #
#! /usr/bin/perl -w

use strict;

@ARGV = glob(join(' ', @ARGV));

while () {
print processing line ($_) in file $ARGV\n;
}

# END multiplefiles2.pl #

Now you can call this script like this:
  multiplefiles2.pl myfile*.pl some*stuff.txt file2

...and Perl will do the rest for you.

By the way: I'd recommend to change the first lines of your script to:

#! /usr/bin/perl -w

use strict;

This enables some more warnings and checks - very helpful to avoid
mistakes...
Also, I'd indent blocks for better readability  - something like:

if ($str2 eq $str1) {
foreach (@values) {
print doing something.\n;
}
}

HTH,

Philipp

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




File handling

2003-02-13 Thread Pankaj Kapare
Hi
I want to to file handling in perl.In that I  have one file which contains some 
lines.Now I want to modify some of lines like..
My file conatins 
line one
line two
line three
password=password;
Login Id =xyz
line 6
and so on

if i want to write new passord and login id then how can i do that? Can  anybody help 
me with sample code 
Thanks in advance!
Pankaj



RE: File handling

2003-02-13 Thread Bob Showalter
Pankaj Kapare wrote:
 Hi
 I want to to file handling in perl.In that I  have one file
 which contains some lines.Now I want to modify some of lines like..
 My file conatins line one
 line two
 line three
 password=password;
 Login Id =xyz
 line 6
 and so on
 
 if i want to write new passord and login id then how can i do
 that? Can  anybody help me with sample code
 Thanks in advance!
 Pankaj

Start with the FAQ article:

   perldoc -q 'change one line'

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File handling

2003-02-13 Thread Paul Kraus
Not tested. I am sure there is a more elegant way to do this but this
should do the trick. It will create a new file with the changes. If you
want to edit it inline you will have to do some research.


#Open files for input and output
open (IN,myfile) or die Can not open file\n;
open (OUT,outfile) or die Can not open file for writing\n;
my ($password,$login)=('password','login');
while(IN){
chomp;
#Assuming that password is at start of line and only
thing on line.
if (/^password=/){
print OUT password=\$password\;;
next;
}
if (/^login=/){
print OUT Login Id = \$login\;;
next;
}
print OUT $_\n;
}


-Original Message-
From: Bob Showalter [mailto:[EMAIL PROTECTED]] 
Sent: Thursday, February 13, 2003 10:56 AM
To: 'Pankaj Kapare'; [EMAIL PROTECTED]
Subject: RE: File handling


Pankaj Kapare wrote:
 Hi
 I want to to file handling in perl.In that I  have one file which 
 contains some lines.Now I want to modify some of lines like.. My file 
 conatins line one line two
 line three
 password=password;
 Login Id =xyz
 line 6
 and so on
 
 if i want to write new passord and login id then how can i do that? 
 Can  anybody help me with sample code Thanks in advance!
 Pankaj

Start with the FAQ article:

   perldoc -q 'change one line'

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File handling

2003-02-13 Thread Paul Kraus
This doesn't work with activestate on windows.

 perldoc -q 'change one line'

Output
No documentation for perl FAQ keyword `'change' found.

-Original Message-
From: Bob Showalter [mailto:[EMAIL PROTECTED]] 
Sent: Thursday, February 13, 2003 10:56 AM
To: 'Pankaj Kapare'; [EMAIL PROTECTED]
Subject: RE: File handling


Pankaj Kapare wrote:
 Hi
 I want to to file handling in perl.In that I  have one file which 
 contains some lines.Now I want to modify some of lines like.. My file 
 conatins line one line two
 line three
 password=password;
 Login Id =xyz
 line 6
 and so on
 
 if i want to write new passord and login id then how can i do that? 
 Can  anybody help me with sample code Thanks in advance!
 Pankaj

Start with the FAQ article:

   perldoc -q 'change one line'

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File handling

2003-02-13 Thread Bob Showalter
Paul Kraus wrote:
 This doesn't work with activestate on windows.
 
  perldoc -q 'change one line'
 
 Output
 No documentation for perl FAQ keyword `'change' found.

Use double quotes for brain-dead windoze:

   perldoc -q change one line

(I just noticed this FAQ answer has changed significantly from 5.6.1 to 5.8)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File handling

2003-02-13 Thread Ramón Chávez
Try Here:

http://www.perldoc.com/perl5.6/pod/perlfaq5.html

-rm-
- Original Message -
From: Paul Kraus [EMAIL PROTECTED]
To: 'Bob Showalter' [EMAIL PROTECTED]; 'Pankaj Kapare'
[EMAIL PROTECTED]; [EMAIL PROTECTED]
Sent: Thursday, February 13, 2003 10:55 AM
Subject: RE: File handling


 This doesn't work with activestate on windows.

  perldoc -q 'change one line'

 Output
 No documentation for perl FAQ keyword `'change' found.

 -Original Message-
 From: Bob Showalter [mailto:[EMAIL PROTECTED]]
 Sent: Thursday, February 13, 2003 10:56 AM
 To: 'Pankaj Kapare'; [EMAIL PROTECTED]
 Subject: RE: File handling


 Pankaj Kapare wrote:
  Hi
  I want to to file handling in perl.In that I  have one file which
  contains some lines.Now I want to modify some of lines like.. My file
  conatins line one line two
  line three
  password=password;
  Login Id =xyz
  line 6
  and so on
 
  if i want to write new passord and login id then how can i do that?
  Can  anybody help me with sample code Thanks in advance!
  Pankaj

 Start with the FAQ article:

perldoc -q 'change one line'

 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]


 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: File Handling

2003-01-15 Thread John W. Krahn
Colin Johnstone wrote:
 
 Gidday all,

Hello,

 Im having trouble reading and writing to this file, It's probably just
 something silly.
 Heres my code. I have set the permissions on the file to 775.
 
 code
 my ($filename);
 
 $filename =
 /home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt;

You can declare and assign in one statement:

my $filename =
'/home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt';


 if( -e $filename ){
   print Yeah - File existsbr;
 }
 
 my @participants;
 
 open IN, $filename || die( Cannot Open: $! );

The high precedence of || means that that in interpreted as:

open IN, ( $filename || die( Cannot Open: $! ) );

Which is not what you want.  You should use the low precedence or
instead:

open IN, $filename or die Cannot Open: $!;

Or use parenthesis for the open function:

open( IN, $filename ) || die Cannot Open: $!;


 while( my $line = IN ){
   chomp $line;
   push( @participants, $line );
 }

You can declare and assign to the array in one statement:

chomp( my @participants = IN );


 close IN;
 
 print Num elements array participants = .scalar( @participants ).br;
 
 my $outString;
 $outString  = ;
 $outString .= $fields{'SelectCity'} . ,;
 $outString .= $fields{'Workshop1'} . ,;
 $outString .= $fields{'Workshop2'} . ,;
 $outString .= $fields{'Salutation'} . ,;
 $outString .= $fields{'FirstName'} . ,;
 $outString .= $fields{'LastName'} . ,;
 $outString .= $fields{'Title'} . ,;
 $outString .= $fields{'CompanyName'} . ,;
 $outString .= $fields{'CompanyAddress'} . ,;
 $outString .= $fields{'Suburb'} . ,;
 $outString .= $fields{'State'} . ,;
 $outString .= $fields{'PostCode'} . ,;
 $outString .= $fields{'PhoneNumber'} . ,;
 $outString .= $fields{'Mobile'} . ,;
 $outString .= $fields{'EmailAddress'};

It looks like you need a join here:

my $outString = join ',',
@fields{ qw( SelectCity Workshop1 Workshop2 Salutation FirstName
 LastName Title CompanyName CompanyAddress Suburb State
 PostCode PhoneNumber Mobile EmailAddress ) };


 print Out string =$outStringbr;
 
 push( @participants, $outString );
 
 print Num elements array participants = .scalar( @participants ).br;
 
 print @participants\n\n;
 
 open( OUTFILE, '$filename') or die( Cannot open file: $!);
 while( @participants ){
   my $val = shift( @participants );
   print( OUTFILE $val\n );
 }

A foreach loop is more efficient then shifting each element from an
array:

foreach( @participants ) {
  print OUTFILE $_\n ;
}


 close( OUTFILE ) or die( Cannot close file: $!);
 /code



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File Handling

2003-01-15 Thread Colin Johnstone
John,

Thank you for your help.

Colin

-Original Message-
From: John W. Krahn [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, 15 January 2003 11:00 PM
To: [EMAIL PROTECTED]
Subject: Re: File Handling


Colin Johnstone wrote:

 Gidday all,

Hello,

 Im having trouble reading and writing to this file, It's probably just
 something silly.
 Heres my code. I have set the permissions on the file to 775.

 code
 my ($filename);

 $filename =
 /home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt;

You can declare and assign in one statement:

my $filename =
'/home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt';


 if( -e $filename ){
   print Yeah - File existsbr;
 }

 my @participants;

 open IN, $filename || die( Cannot Open: $! );

The high precedence of || means that that in interpreted as:

open IN, ( $filename || die( Cannot Open: $! ) );

Which is not what you want.  You should use the low precedence or
instead:

open IN, $filename or die Cannot Open: $!;

Or use parenthesis for the open function:

open( IN, $filename ) || die Cannot Open: $!;


 while( my $line = IN ){
   chomp $line;
   push( @participants, $line );
 }

You can declare and assign to the array in one statement:

chomp( my @participants = IN );


 close IN;

 print Num elements array participants = .scalar( @participants ).br;

 my $outString;
 $outString  = ;
 $outString .= $fields{'SelectCity'} . ,;
 $outString .= $fields{'Workshop1'} . ,;
 $outString .= $fields{'Workshop2'} . ,;
 $outString .= $fields{'Salutation'} . ,;
 $outString .= $fields{'FirstName'} . ,;
 $outString .= $fields{'LastName'} . ,;
 $outString .= $fields{'Title'} . ,;
 $outString .= $fields{'CompanyName'} . ,;
 $outString .= $fields{'CompanyAddress'} . ,;
 $outString .= $fields{'Suburb'} . ,;
 $outString .= $fields{'State'} . ,;
 $outString .= $fields{'PostCode'} . ,;
 $outString .= $fields{'PhoneNumber'} . ,;
 $outString .= $fields{'Mobile'} . ,;
 $outString .= $fields{'EmailAddress'};

It looks like you need a join here:

my $outString = join ',',
@fields{ qw( SelectCity Workshop1 Workshop2 Salutation FirstName
 LastName Title CompanyName CompanyAddress Suburb State
 PostCode PhoneNumber Mobile EmailAddress ) };


 print Out string =$outStringbr;

 push( @participants, $outString );

 print Num elements array participants = .scalar( @participants ).br;

 print @participants\n\n;

 open( OUTFILE, '$filename') or die( Cannot open file: $!);
 while( @participants ){
   my $val = shift( @participants );
   print( OUTFILE $val\n );
 }

A foreach loop is more efficient then shifting each element from an
array:

foreach( @participants ) {
  print OUTFILE $_\n ;
}


 close( OUTFILE ) or die( Cannot close file: $!);
 /code



John
--
use Perl;
program
fulfillment

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




File Handling

2003-01-14 Thread Colin Johnstone
Gidday all,

Im having trouble reading and writing to this file, It's probably just
something silly.
Heres my code. I have set the permissions on the file to 775.

code
my ($filename);

$filename =
/home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt;

if( -e $filename ){
  print Yeah - File existsbr;
}

my @participants;

open IN, $filename || die( Cannot Open: $! );
while( my $line = IN ){
  chomp $line;
  push( @participants, $line );
}
close IN;

print Num elements array participants = .scalar( @participants ).br;

my $outString;
$outString  = ;
$outString .= $fields{'SelectCity'} . ,;
$outString .= $fields{'Workshop1'} . ,;
$outString .= $fields{'Workshop2'} . ,;
$outString .= $fields{'Salutation'} . ,;
$outString .= $fields{'FirstName'} . ,;
$outString .= $fields{'LastName'} . ,;
$outString .= $fields{'Title'} . ,;
$outString .= $fields{'CompanyName'} . ,;
$outString .= $fields{'CompanyAddress'} . ,;
$outString .= $fields{'Suburb'} . ,;
$outString .= $fields{'State'} . ,;
$outString .= $fields{'PostCode'} . ,;
$outString .= $fields{'PhoneNumber'} . ,;
$outString .= $fields{'Mobile'} . ,;
$outString .= $fields{'EmailAddress'};

print Out string =$outStringbr;

push( @participants, $outString );

print Num elements array participants = .scalar( @participants ).br;

print @participants\n\n;

open( OUTFILE, '$filename') or die( Cannot open file: $!);
while( @participants ){
  my $val = shift( @participants );
  print( OUTFILE $val\n );
}
close( OUTFILE ) or die( Cannot close file: $!);
/code

Here is a sample of the output I get on the screen

output
Yeah - File exists
Num elements array participants = 2
Out string =Melbourne,Next Generation Alpha Servers - Simplifying Cluster
Management with Single System Image,HP Single Server Strategy - Itanium is
real,vcxzvcxzv,vcxzvcxz,vxzcvcxz,zxcvcxzvxc,vzcxvzcx,xczvcxzv,vzxcvzcxv,vcxv
zcxv,vzxcvzcxv,vzcxvxzcv,vzxcvzxcvz,[EMAIL PROTECTED]
Num elements array participants = 3
Melbourne,Next Generation Alpha Servers - Simplifying Cluster Management
with Single System Image,HP Single Server Strategy - Itanium is
real,vcxzvcxzv,vcxzvcxz,vxzcvcxz,zxcvcxzvxc,vzcxvzcx,xczvcxzv,vzxcvzcxv,vcxv
zcxv,vzxcvzcxv,vzcxvxzcv,vzxcvzxcvz,[EMAIL PROTECTED]
/output

Im pulling my hair out, maybe I've been at it too long today, Im going to
bed, hopefully the morning will bring a solution.

Any help appreciated.

Colin



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File Handling

2003-01-14 Thread Dan Muey
Try using double quotes around the $filename part when you open it to write.

Dan

-Original Message-
From: Colin Johnstone [mailto:[EMAIL PROTECTED]] 
Sent: Tuesday, January 14, 2003 8:32 AM
To: [EMAIL PROTECTED]
Subject: File Handling


Gidday all,

Im having trouble reading and writing to this file, It's probably just something 
silly. Heres my code. I have set the permissions on the file to 775.

code
my ($filename);

$filename = /home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt;

if( -e $filename ){
  print Yeah - File existsbr;
}

my @participants;

open IN, $filename || die( Cannot Open: $! );
while( my $line = IN ){
  chomp $line;
  push( @participants, $line );
}
close IN;

print Num elements array participants = .scalar( @participants ).br;

my $outString;
$outString  = ;
$outString .= $fields{'SelectCity'} . ,;
$outString .= $fields{'Workshop1'} . ,;
$outString .= $fields{'Workshop2'} . ,;
$outString .= $fields{'Salutation'} . ,;
$outString .= $fields{'FirstName'} . ,;
$outString .= $fields{'LastName'} . ,;
$outString .= $fields{'Title'} . ,;
$outString .= $fields{'CompanyName'} . ,;
$outString .= $fields{'CompanyAddress'} . ,;
$outString .= $fields{'Suburb'} . ,;
$outString .= $fields{'State'} . ,;
$outString .= $fields{'PostCode'} . ,;
$outString .= $fields{'PhoneNumber'} . ,;
$outString .= $fields{'Mobile'} . ,;
$outString .= $fields{'EmailAddress'};

print Out string =$outStringbr;

push( @participants, $outString );

print Num elements array participants = .scalar( @participants ).br;

print @participants\n\n;

open( OUTFILE, '$filename') or die( Cannot open file: $!); while( @participants ){
  my $val = shift( @participants );
  print( OUTFILE $val\n );
}
close( OUTFILE ) or die( Cannot close file: $!);
/code

Here is a sample of the output I get on the screen

output
Yeah - File exists
Num elements array participants = 2
Out string =Melbourne,Next Generation Alpha Servers - Simplifying Cluster Management 
with Single System Image,HP Single Server Strategy - Itanium is 
real,vcxzvcxzv,vcxzvcxz,vxzcvcxz,zxcvcxzvxc,vzcxvzcx,xczvcxzv,vzxcvzcxv,vcxv
zcxv,vzxcvzcxv,vzcxvxzcv,vzxcvzxcvz,[EMAIL PROTECTED]
Num elements array participants = 3
Melbourne,Next Generation Alpha Servers - Simplifying Cluster Management with Single 
System Image,HP Single Server Strategy - Itanium is 
real,vcxzvcxzv,vcxzvcxz,vxzcvcxz,zxcvcxzvxc,vzcxvzcx,xczvcxzv,vzxcvzcxv,vcxv
zcxv,vzxcvzcxv,vzcxvxzcv,vzxcvzxcvz,[EMAIL PROTECTED]
/output

Im pulling my hair out, maybe I've been at it too long today, Im going to bed, 
hopefully the morning will bring a solution.

Any help appreciated.

Colin



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: File Handling

2003-01-14 Thread Rob Dixon
Hi Colin

It looks like your file contains two blank lines to start with. The data
from your hash should be appended to it after the run. Try dumping your
array with:

print $_\n foreach @participants;

then you can see where each record starts and ends.

HTH,

Rob


Colin Johnstone [EMAIL PROTECTED] wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
 Gidday all,

 Im having trouble reading and writing to this file, It's probably just
 something silly.
 Heres my code. I have set the permissions on the file to 775.

 code
 my ($filename);

 $filename =
 /home/bdweb8083m/johnstonefamily.com/cgi-bin/hp_data/participants.txt;

 if( -e $filename ){
   print Yeah - File existsbr;
 }

 my @participants;

 open IN, $filename || die( Cannot Open: $! );
 while( my $line = IN ){
   chomp $line;
   push( @participants, $line );
 }
 close IN;

 print Num elements array participants = .scalar( @participants ).br;

 my $outString;
 $outString  = ;
 $outString .= $fields{'SelectCity'} . ,;
 $outString .= $fields{'Workshop1'} . ,;
 $outString .= $fields{'Workshop2'} . ,;
 $outString .= $fields{'Salutation'} . ,;
 $outString .= $fields{'FirstName'} . ,;
 $outString .= $fields{'LastName'} . ,;
 $outString .= $fields{'Title'} . ,;
 $outString .= $fields{'CompanyName'} . ,;
 $outString .= $fields{'CompanyAddress'} . ,;
 $outString .= $fields{'Suburb'} . ,;
 $outString .= $fields{'State'} . ,;
 $outString .= $fields{'PostCode'} . ,;
 $outString .= $fields{'PhoneNumber'} . ,;
 $outString .= $fields{'Mobile'} . ,;
 $outString .= $fields{'EmailAddress'};

 print Out string =$outStringbr;

 push( @participants, $outString );

 print Num elements array participants = .scalar( @participants ).br;

 print @participants\n\n;

 open( OUTFILE, '$filename') or die( Cannot open file: $!);
 while( @participants ){
   my $val = shift( @participants );
   print( OUTFILE $val\n );
 }
 close( OUTFILE ) or die( Cannot close file: $!);
 /code

 Here is a sample of the output I get on the screen

 output
 Yeah - File exists
 Num elements array participants = 2
 Out string =Melbourne,Next Generation Alpha Servers - Simplifying Cluster
 Management with Single System Image,HP Single Server Strategy - Itanium is

real,vcxzvcxzv,vcxzvcxz,vxzcvcxz,zxcvcxzvxc,vzcxvzcx,xczvcxzv,vzxcvzcxv,vcxv
 zcxv,vzxcvzcxv,vzcxvxzcv,vzxcvzxcvz,[EMAIL PROTECTED]
 Num elements array participants = 3
 Melbourne,Next Generation Alpha Servers - Simplifying Cluster Management
 with Single System Image,HP Single Server Strategy - Itanium is

real,vcxzvcxzv,vcxzvcxz,vxzcvcxz,zxcvcxzvxc,vzcxvzcx,xczvcxzv,vzxcvzcxv,vcxv
 zcxv,vzxcvzcxv,vzcxvxzcv,vzxcvzxcvz,[EMAIL PROTECTED]
 /output




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




file handling syntax

2002-02-09 Thread Steve Doerr

Greetings!  Could someone here offer a tip about declaring a file
variable that is itself part variable?

I can't seem to find an example of this online and I hope someone here
could help.

I'd like to put a path variable in the declaration, but can't seem to
get it to work.

What would the proper syntax for this be?

$file=$form-{path}tbdump.csv;
open(tbdump, $file);
print tbdump Check that it writes the file.\n;
close(tbdump);

Thanks very much for any input.
Steve


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: file handling syntax

2002-02-09 Thread John W. Krahn

Steve Doerr wrote:
 
 Greetings!  Could someone here offer a tip about declaring a file
 variable that is itself part variable?
 
 I can't seem to find an example of this online and I hope someone here
 could help.
 
 I'd like to put a path variable in the declaration, but can't seem to
 get it to work.
 
 What would the proper syntax for this be?
 
 $file=$form-{path}tbdump.csv;

my $file = $form-{path} . 'tbdump.csv';

 open(tbdump, $file);

open TBDUMP, $file or die Cannot open $file: $!;

 print tbdump Check that it writes the file.\n;

print TBDUMP Check that it writes the file.\n;

 close(tbdump);

close TBDUMP;



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File Handling question - easy

2001-08-03 Thread Bob Showalter

 -Original Message-
 From: Michael Fowler [mailto:[EMAIL PROTECTED]]
 Sent: Thursday, August 02, 2001 2:48 PM
 To: Bob Showalter
 Cc: [EMAIL PROTECTED]
 Subject: Re: File Handling question - easy
 
 
 On Thu, Aug 02, 2001 at 02:31:28PM -0400, Bob Showalter wrote:
  P.S. I'm surprised this works with while(). I didn't 
 realize fileglobs
  were magical inside while(), but it appears they are... Is 
 this documented?
 
 Yes, perldoc perlop, in the I/O Operators section (5.6.1 version):
 
   A (file)glob evaluates its (embedded) argument only when it is
   starting a new list.  All values must be read before it 
 will start
   over.  In list context, this isn't important because 
 you automatically
   get them all anyway.  However, in scalar context the 
 operator returns
   the next value each time it's called, or undef when 
 the list has run
   out.  As with filehandle reads, an automatic defined 
 is generated
   when the glob occurs in the test part of a while, 
 because legal glob
   returns (e.g. a file called 0) would otherwise 
 terminate the loop.
   Again, undef is returned only once.

Thanks for the cite.

I played around with this a bit and found that:

   perl -e 'print scalar(glob(*)) for (1..2)'

prints two different files, while

   perl -e 'print scalar(glob(*)), scalar(glob(*))'

prints the same file twice. I tried this on 5.005_03 and 5.6.1.

Wonder why? Seems like its related to the loop context and not merely
to scalar context.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: File Handling question - easy

2001-08-03 Thread Michael Fowler

On Thu, Aug 02, 2001 at 03:00:24PM -0400, Bob Showalter wrote:
 I played around with this a bit and found that:
 
perl -e 'print scalar(glob(*)) for (1..2)'
 
 prints two different files, while
 
perl -e 'print scalar(glob(*)), scalar(glob(*))'
 
 prints the same file twice. I tried this on 5.005_03 and 5.6.1.
 
 Wonder why? Seems like its related to the loop context and not merely
 to scalar context.

It has to do with it being the same operator; you're effectively starting a
new glob with the second scalar(glob '*').  I assume there is some context
assigned to the op code itself.  Consider a similar example to your for
loop:

{
my $file = glob('*');
last unless defined($file);

print $file;

redo;
}


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




File Handling question - easy

2001-08-03 Thread Jon

I don't know if I sent my first email correctly.  Sorry for the repeat if I 
did, I'm new :)

Hello,
The code below takes all files in my current directory that have filenames 
ending with .txt. Using this stuff I can loop through those files and do 
what I need to do.

My problem is, I want to do this for a bunch of different patterns (ex. 
.txt, .this, .that). How can I make what is inside the angle brackets 
a variable? I've tried to use eval to build my while statement and I can't 
make it work. Help Please?

while(*.txt)
{
### do stuff, given a filename
}

Jon


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: File Handling question - easy

2001-08-02 Thread Michael Fowler

On Thu, Aug 02, 2001 at 11:03:23AM -0700, Jon wrote:
 My problem is, I want to do this for a bunch of different patterns (ex. 
 .txt, .this, .that).

You could say *.txt *.this *.that, or you can use opendir, readdir, and a
regex (perhaps grep).


 How can I make what is inside the angle brackets a variable?

You don't, it can confuse Perl and users having to look at your code.  Use
glob() instead of angle brackets, in this case.

$pats = *.txt *.this *.that;
print join(\n, glob($pats);


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: File Handling question - easy

2001-08-02 Thread Jon

Bob-Mike-Jerry,
Thanks guys

Jon

At 01:36 PM 8/2/01 -0500, Jerry Preston wrote:
Jon,

Read all the files in the directory then go through them based on the
file ext
and do what you want.

   opendir( PROGRAM, ./ );
   foreach $program ( readdir( PROGRAM )) {
 if( $program =~ .txt ) {
### do stuff, given a filename
 }elsif( $program =~ .this ) {
### do stuff, given a filename
 }
   }

Jerry


Jon wrote:
 
  I don't know if I sent my first email correctly.  Sorry for the repeat if I
  did, I'm new :)
 
  Hello,
  The code below takes all files in my current directory that have filenames
  ending with .txt. Using this stuff I can loop through those files and do
  what I need to do.
 
  My problem is, I want to do this for a bunch of different patterns (ex.
  .txt, .this, .that). How can I make what is inside the angle brackets
  a variable? I've tried to use eval to build my while statement and I can't
  make it work. Help Please?
 
  while(*.txt)
  {
  ### do stuff, given a filename
  }
 
  Jon
 
  --
  To unsubscribe, e-mail: [EMAIL PROTECTED]
  For additional commands, e-mail: [EMAIL PROTECTED]

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: File Handling question - easy

2001-08-02 Thread Michael Fowler

On Thu, Aug 02, 2001 at 02:31:28PM -0400, Bob Showalter wrote:
 P.S. I'm surprised this works with while(). I didn't realize fileglobs
 were magical inside while(), but it appears they are... Is this documented?

Yes, perldoc perlop, in the I/O Operators section (5.6.1 version):

  A (file)glob evaluates its (embedded) argument only when it is
  starting a new list.  All values must be read before it will start
  over.  In list context, this isn't important because you automatically
  get them all anyway.  However, in scalar context the operator returns
  the next value each time it's called, or undef when the list has run
  out.  As with filehandle reads, an automatic defined is generated
  when the glob occurs in the test part of a while, because legal glob
  returns (e.g. a file called 0) would otherwise terminate the loop.
  Again, undef is returned only once.


Michael
--
Administrator  www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




RE: File Handling question - easy

2001-08-02 Thread Jon

Bob,
I got it from Perl Black Book by S. Holzner, section on globs.  Very very 
good book to learn Perl from, extremely easy to read, excellent for 
beginners and I'm not going to know this for a long while but I think it is 
an excellent book for intermediate Perl dudes too.  End of section reads 
like this:

Let me give you a useful hint: Using the glob function internally, Perl 
allows you to write expressions like this, which will print the names of 
the files with the extension .txt:

while (*.txt) {
 print;
}




  while(*.txt)
  {
  ### do stuff, given a filename
  }

Use glob():

my $patterns = '*.txt *.this *.that';
for (glob($patterns)) { ... }

P.S. I'm surprised this works with while(). I didn't realize fileglobs
were magical inside while(), but it appears they are... Is this documented?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]