Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Instream Input Data 1

Status
Not open for further replies.

canguro

Programmer
Sep 15, 2002
57
0
0
US
Hello,

I am curious to know if it is possible to have input data placed directly 'instream' in the program, in other words, whether it is possible to paste the data right into my program and run against it (I normally execute my scripts from within my Perl editor). I know this may seem strange, but this capability could be useful to me. I do not envision an enormous amount of data --- perhaps 50-100 lines.

I checked in the indexes in the 2 books I have on Perl and could not find any references to this concept. Also, I did a 'search' in this Forum and could not find an answer.

Any help would be appreciated. Many thanks.

 
canguro,

I'm not sure I understand the question. You wan't to be able to act on data, like either a flat file, or database in a CGI/Web environment?

Or do you mean to be able to run a script against a file
Code:
perl myscript.pl start=10 file=myfile.txt

myscript.pl
Code:
#!/usr/bin/perl

use CGI;
use strict;
my $q=new CGI;
my $start=$q->param('start');
my $file =$q->param('file');
my $basedir="/path/to/file";
open FH, "<$basedir/$file";
my $loop=0;
while (<FH>) {
  if ($loop > $start) {
     print $_;
  }
  $loop++;
}

Without using CGI, you can access the parameter list through @ARGV, $ARGV[0] being the first, and so on

HTH
--Paul

cigless ...
 
Paul,

Thanks for your reply. From time to time I have some raw text-format data in Windows Notepad. I would love to be able to place ('copy-paste') the data right into my script and let the script run against it and immediately edit it. Here is a simple-minded visualization of my thought process for the Perl script:

1. Initialization code, if needed
2. Code which tells Perl the data to be read follows right
here. Read it all and place in a holding-area (e.g., if
I have 75 lines, read this instream data and place in a
75-element array)
3. Next, process (read) the array just constructed 1 element
at a time and perform editing.
4. Stop when array is exhuasted

I'm sorry if this seems vague, but it would be useful to do the 'paste' of text data into my edit-script and do an immediate run. If this is impossible then it's not a big deal.

Thanks again for giving this some thought.

Joe


 
Code:
while (<DATA>) {
  (@data)=split /\s+/, $_;
   foreach (@data) {
     print "$_\n";
   }
}
__DATA__
xxxx xxxxxxx xxxxx xxxxxx
xxxx xxxxxxx xxxxx xxxxxx
xxxx xxxxxxx xxxxx xxxxxx
xxxx xxxxxxx xxxxx xxxxxx
xxxx xxxxxxx xxxxx xxxxxx
your data here
xxxx xxxxxxx xxxxx xxxxxx
xxxx xxxxxxx xxxxx xxxxxx
xxxx xxxxxxx xxxxx xxxxxx

Sounds like you're looking for the __DATA__ handle which you put at the end of your script

HTH
--Paul

cigless ...
 
Paul,

Thank you for your suggestion. That may be what I need. I will give it a try.

Joe
 
Thats it.... just put the:

Code:
__DATA__
your raw text lines here

at the end of your perl script, I use it all the time for testing code.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top