Page 1 of 1

Perl newbie (don't follow <> notation ????)

Posted: Wed Aug 04, 2004 8:08 am
by Chris Corbyn
Hi,

I'm just starting out in Perl and I'm trying to loop over lines of text to do this task....
Write a script which assembles separate lines in to longer lines. A new line in the output is one that starts with at least two digits. All the lines which follow it, until the next occurrence of two digits at the start of a line, are a continuation of the earlier line. Eg

21 this
is all part
of
the same line
223 but this is different
this was the given solution to the problem....

Code: Select all

$var = "";
	while (<>) &#123;
		if (/^\d\d/) &#123;
			print "$var\n";
			$var = ""
		&#125;
		$var .= $_;
	&#125;
	print "$var\n";		# any leftover?
and it noted that <> means "loop over these lines of text"

So I tried putting some text inside the <> and all it did at the end was printed the whole string same as the input.

Code: Select all

$var = "";
	while (<123 a line of text \nthis line should be the same as the first \n145 but this one should be different>) &#123;
		if (/^\d\d/) &#123;
			print "$var\n";
			$var = ""
		&#125;
		$var .= $_;
	&#125;
	print "$var\n";		# any leftover?
I am aware that the above which is my version is meaningless but I'm sure if somebody could word it nicely I'd understand how to use the real solution that was given :-)

Also where do I use ^D?

Thanks :-)

Posted: Wed Aug 04, 2004 8:09 am
by Chris Corbyn
Is it intended to open a file and read the text in the file? How is that done?

Posted: Wed Aug 04, 2004 10:48 am
by Chris Corbyn
My dumbness

Code: Select all

open(FH,"<somfile.txt");

while (<FH>) &#123;  # yadda yadda yadda
Do you have to be working with file contents though? Isn't it possible to create some variable with a long string containing \n and use that?

Posted: Thu Aug 05, 2004 11:27 am
by Weirdan
d11wtq wrote:My dumbness

Code: Select all

open(FH,"<somfile.txt");

while (<FH>) &#123;  # yadda yadda yadda
Do you have to be working with file contents though?
perldoc.com wrote: In scalar context, evaluating a filehandle in angle brackets yields the next line from that file (the newline, if any, included), or undef at end-of-file or on error.
Isn't it possible to create some variable with a long string containing \n and use that?
something like this:

Code: Select all

$asd = "23asdasd\nsdfsdf\nqwe";

   $var = "";
   for (split(/\n/, $asd)) &#123;
      if (/^\d\d/) &#123;
         print "$var\n";
         $var = ""
      &#125;
      $var .= $_;
   &#125;
   print "$var\n";      # any leftover?

Re: Perl newbie (don't follow <> notation ????)

Posted: Thu Aug 05, 2004 12:23 pm
by timvw
d11wtq wrote:Hi,

I'm just starting out in Perl and I'm trying to loop over lines of text to do this task....

Write a script which assembles separate lines in to longer lines. A new line in the output is one that starts with at least two digits. All the lines which follow it, until the next occurrence of two digits at the start of a line, are a continuation of the earlier line. Eg

21 this
is all part
of
the same line
223 but this is different
Thus you start with coming up with an algorithm.
One would be: as long as there is input, read in the line.
If the line starts with 2 decimals, append it to what we have.
Else add the previous stuff to the results, and build a new string

Code: Select all

my @lines; # here we store the results

my $current = ''; # to start we have nothing
while (<>)   # while there are lines we read from <STDIN> into $_ 
&#123; 
  if (/^\d\d) # the line starts with 2 decimals 
  &#123; 
     $current .= $_; # we append
  &#125;
  else 
  &#123;
     push(@lines, $current); # what we currently have is a complete line
     $current = $_; # we start to build a new line
  &#125;
&#125;

# done ;)

Posted: Fri Aug 06, 2004 9:12 pm
by Chris Corbyn
Thanks guys.

Got it figured in the end. I'm having a little trouble with a pattern match to extract hyperlink information from html documents now but i've posted that in a separate post so I shouldn't really mention it here.

Thanks again :-)