Stephan Hochhaus wrote:
> Hello list,
> 
> I am trying to write my very own first "useful" program and I am in
> need of some help. I do have a lot of ressources (a book by Andrew
> Johnson, the yellow book for dummies and lots of hypertexts) but I
> cannot seem to get things done the way they're supposed to work. I am
> completely new to Perl and programming as well.
> 
> So here's what I want to do:
> I want to write a little script that will read the files from a
> directory and make html links out of it. The data files look somewhat
> like this: 
> 
> Datafile:
> Linkname\tDescription\tURL\tLanguage

OK, looks good.

> 
> Here's what I have so far:
> #!/usr/bin/perl
> use strict;
> use diagnostics;
> 
> #read filenames
> my @filenames = glob"./data/*";
> print "filenames read...\n";
> #sort files
> @filenames = sort @filenames;
> print "@filenames\n";
> 
> #output link for every file
> foreach my $filename (@filenames) {
>     open(FILE, $filename) or die "cannot open $filename!\n";    
>        while (<FILE>) { #read every single lines and output HTML
>        print "$filename\n";

The while(<FILE>) reads a line into the $_ variable, but you're not printing
that; you're printing the filename itself. So you'll get one line of (the
same) output for each line in the file.

If you mean to print each filename as it's being processed, move the print
statement up above the while() line.

>        }
>     close FILE ;
>     }
> 
> My questions so far (please don't give me a complete
> solution, I wanna
> work my way into Perl) is, why does the last print
> "$filename\n"; print
> every filename twice? 

See above.

> And how can I get rid of the directoy
> info up to
> the last / (./data/perl.txt)? Will that require knowledge of the
> mystical RegExp? 

You can use a regex, but the "preferred" way is to use the standard
File::Basename module. All you do is add the following line at the top of
your script:

   use File::Basename;

Then to show just the 'perl.txt' part, use:

   print basename($filename), "\n";

> How can I get the tab delimited strings in my
> datafile into 
> an array

Use the split() function. split() has a "default" mode that splits on
whitespace (like awk). If your fields contain spaces that's no good; you
want to split only on the tab char. In addition, since $_ contains the line
terminator when read by while(<FILE>), you need to chomp() that off.

So you do something like this:

   while (<FILE>) {
       chomp;                 # remove line terminator
       my @f = split /\t/;    # split into separate fields on tab char

   }

> so
> that I can access that array like this: print "<a
> href="$hyperlink[3]">"; and so on?
> 
> I hope this is not too trivial, but I've been trying to nail things
> down for the last two days, I am glad I found this diagnostics option
> but now I am stuck.. 
> 
> Any help is greatly appreciated!
> 
> Stephan - who has never written a programm before.

You're off to a good start!

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

Reply via email to