Converting file-input argument as string

J

Jürgen Exner

Edward said:
Suppose I have this snippet
which take files as input argument.

I guess you mean the arguments are file names, not files?
$file = $ARGV[0];
open(FILE,"<$file");
@array = <FILE>;
close(FILE);
Is there anyway we can convert $file
into strings (of file name)?

There is no variable type "string" in Perl. $file is a scalar. And any
scalar has a text representation.
So, for all practical purposes $file _is_ a string already and can be
treated just like a string (it's also a number, but that is irrelevant
here).
In so far you question doesn't make sense. What are you trying to do?

jue
 
A

A. Sinan Unur

Suppose I have this snippet
which take files as input argument.

use strict;
use warnings;
$file = $ARGV[0];

my $file = $ARGV[0];
open(FILE,"<$file");

Always check whether open succeeded. I prefer lexical filehandles and the
three argument form of open:

open my $in, '<', $file or die "Error opening $file: $!";
@array = <FILE>;

Why are you slurping the file?
close(FILE);

Is there anyway we can convert $file into strings (of file name)?

Jurgen already asked what you mean by that. In your response to him, you
stated:

I would like to use that string as a name of output files
(with some added string as index).

open my $out, '>', $file.'.ext' or die "Error opening $file.ext: $!";

I will ask again: What are you trying to do?
 
P

Paul Lalli

Suppose I have this snippet which take files as input argument.
$file = $ARGV[0];
open(FILE,"<$file");
@array = <FILE>;
close(FILE);
Is there anyway we can convert $file into strings (of file name)?

$file *is* a string. What kind of conversion do you think you need? (And
why do you think that?)

print "The file is: $file\n";
I would like to use that string as a name of
output files (with some added string as index).

So what's stopping you? The variable IS a string. Just go ahead and use
it. What have you tried that doesn't work the way you think it should?

my $outfile = $file . '.o'; # add '.o' to end of original filename
open OFILE, '>', $outfile or die "Cannot open $outfile for writing: $!";

Paul Lalli
 
E

Edward Wijaya

Hi,

Suppose I have this snippet
which take files as input argument.

$file = $ARGV[0];
open(FILE,"<$file");
@array = <FILE>;
close(FILE);

Is there anyway we can convert $file
into strings (of file name)?

Because we need this later as string value
for other operation.


Regards
Edward WIJAYA
SINGAPORE
 
J

Jürgen Exner

Edward said:
$file = $ARGV[0];
open(FILE,"<$file");
@array = <FILE>;
close(FILE);
Is there anyway we can convert $file
into strings (of file name)?
In so far you question doesn't make sense. What are you trying to
do?

I would like to use that string [note: $file] as a name of
output files (with some added string as index).

Ok, so what is the problem? For all practical purposes $file _is_ a string.
You can certainly append more text to it, you can split it, you can slice
and dice it any way you like, just like any other string.

Again, what is it you want to do and got stuck with?

jue
 
E

Edward Wijaya

In so far you question doesn't make sense. What are you trying to do?

I would like to use that string as a name of
output files (with some added string as index).


Edward WIJAYA
SINGAPORE
 
K

Kevin Collins

So what's stopping you? The variable IS a string. Just go ahead and use
it. What have you tried that doesn't work the way you think it should?

I always thought $file from $ARGV[0]
is the whole file itself.

Now I know it can work.

Thanks so much.

Edward,

I've posted a similar response to one of your posts in comp.unix.shell, but
I'll try again:

Please, PLEASE start out by attempting to *learn* the languages you are using
before posting all these questions to newsgroups that waste people's time. Read
the man pages, the perldocs, the FAQs, the various tutorial sites, Google.com,
etc.

It has become clear to me that you are in WAY over your head. People will help
you no matter what, since you are polite and seem to try to listen to what they
are saying, but you are pushing the limits of what is acceptable.

Every question I have seen you post (~15+), would be answered in the basic
documentation for the respective language (Perl, ksh), which means you didn't
look or you don't care if you are wasting my time.

For your benefit, I really hope you try to do some learning before you do more
posting.

Good luck,

Kevin
 
E

Edward Wijaya

So what's stopping you? The variable IS a string. Just go ahead and use
it. What have you tried that doesn't work the way you think it should?

I always thought $file from $ARGV[0]
is the whole file itself.

Now I know it can work.

Thanks so much.

Regards
Edward WIJAYA
SINGAPORE
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,269
Messages
2,571,097
Members
48,773
Latest member
Kaybee

Latest Threads

Top