For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > Using a variable as output filename









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Using a variable as output filename
banker123

2006-10-18, 9:59 pm

Please help,
I have a variable from an array that I want to use as the filename for
each line in the array.

Something like this:
The Array (input)
1 Brad
2 Bob
3 Ben

Code:
@array=<input>
foreach $line(@array)
{
set column 1=$file and colum 2=$name
open, output "$file.txt"
print output "$name\n"
}

Output required
Filename Contents of File
1.txt Brad
2.txt Bob
3.txt Ben

usenet@DavidFilmer.com

2006-10-19, 3:59 am

banker123 wrote:
> @array=<input>


Please don't ever, ever, ever, ever do that. It is EXTREMELY rare that
you should populate an entire array from a filehandle. Process the
filehandle one line at a time instead (never using an array).

> foreach $line(@array)
> {
> set column 1=$file and colum 2=$name


I assume that's supposed to be psuedo-code and not actual Perl.

> open, output "$file.txt"


Ditto.

> print output "$name\n"
> }
>
> Output required
> Filename Contents of File
> 1.txt Brad
> 2.txt Bob
> 3.txt Ben


OK, how about something like this:

#!/usr/bin/perl
use strict; use warnings;

while (<DATA> ) {
my ($number, $name) = split;
open (my $out, '>', "/tmp/$number.txt");
print $out "$name\n";
close $out;
}

__DATA__
1 Brad
2 Bob
3 Ben

--
David Filmer (http://DavidFilmer.com)

banker123

2006-10-19, 7:56 am

I tried the code below, no luck. Plese help a newbie!

Open (data, 'C:\Source.txt') or die ("Cannot Open");

while (<DATA> ) {
my ($number, $name) = split ;
open (my $out, '>', "C:\$number.bat");
print $out "$name\n";
close $out;
}

usenet@DavidFilmer.com wrote:
> banker123 wrote:
>
> Please don't ever, ever, ever, ever do that. It is EXTREMELY rare that
> you should populate an entire array from a filehandle. Process the
> filehandle one line at a time instead (never using an array).
>
>
> I assume that's supposed to be psuedo-code and not actual Perl.
>
>
> Ditto.
>
>
> OK, how about something like this:
>
> #!/usr/bin/perl
> use strict; use warnings;
>
> while (<DATA> ) {
> my ($number, $name) = split;
> open (my $out, '>', "/tmp/$number.txt");
> print $out "$name\n";
> close $out;
> }
>
> __DATA__
> 1 Brad
> 2 Bob
> 3 Ben
>
> --
> David Filmer (http://DavidFilmer.com)


Paul Lalli

2006-10-19, 7:56 am

banker123 wrote:
> I tried the code below, no luck. Plese help a newbie!


"no luck" is meaningless. WHAT went wrong? Compiler errors? Runtime
errors? Infinite loop? Segfault? Computer bursts into flames?

>
> Open (data, 'C:\Source.txt') or die ("Cannot Open");


There is no such function as Open. Is this the problem you had? The
function name is open().

You should be using lexical filehandles. You should be using the three
argument form of open. You should be including the *reason* the open
failed in your die message. You should be using normal slashes to
avoid problems with escaping backslashes.

open my $data_fh, '<', 'C:/Source.txt' or die "Cannot open: $!";

>
> while (<DATA> ) {


Above you tried to open the filehandle named data, but here you're
trying to print the filehandle named DATA. For one, those aren't the
same filehandle. For two, DATA is a special filelhandle that reads not
from a file, but from the lines of code belong the __DATA__ marker.

> my ($number, $name) = split ;
> open (my $out, '>', "C:\$number.bat");


Here you are trying to open a file named "C, colon, dollarsign, n, u,
m, b, e, r, dot, b, a, t". Again, stop using backslashes.

open my $out_fh, '>', "C:/$number.bat" or die "Cannot open file: $!";

> print $out "$name\n";
> close $out;
> }


Paul Lalli

banker123

2006-10-19, 6:56 pm

Worked!!

open (file, 'C:\source.txt') or die "Cannot open file: $!";
while (<file> ) {
my ($number, $name) = split ;
open (my $out, '>', "C:\\test\\$number.bat")or die "Cannot open
file: $!";
print $out "$name\n";
close $out;
}

How should I represent the window file structure (not using
backslashes)?


Paul Lalli wrote:
> banker123 wrote:
>
> "no luck" is meaningless. WHAT went wrong? Compiler errors? Runtime
> errors? Infinite loop? Segfault? Computer bursts into flames?
>
>
> There is no such function as Open. Is this the problem you had? The
> function name is open().
>
> You should be using lexical filehandles. You should be using the three
> argument form of open. You should be including the *reason* the open
> failed in your die message. You should be using normal slashes to
> avoid problems with escaping backslashes.
>
> open my $data_fh, '<', 'C:/Source.txt' or die "Cannot open: $!";
>
>
> Above you tried to open the filehandle named data, but here you're
> trying to print the filehandle named DATA. For one, those aren't the
> same filehandle. For two, DATA is a special filelhandle that reads not
> from a file, but from the lines of code belong the __DATA__ marker.
>
>
> Here you are trying to open a file named "C, colon, dollarsign, n, u,
> m, b, e, r, dot, b, a, t". Again, stop using backslashes.
>
> open my $out_fh, '>', "C:/$number.bat" or die "Cannot open file: $!";
>
>
> Paul Lalli


usenet@DavidFilmer.com

2006-10-19, 6:56 pm

banker123 wrote:
> How should I represent the window file structure (not using
> backslashes)?


Just use forward slashes. Windows will figure it out.

--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com