For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > May 2004 > Converting file-input argument as string









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 Converting file-input argument as string
Edward Wijaya

2004-05-20, 11:34 am

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ürgen Exner

2004-05-20, 12:32 pm

Edward Wijaya wrote:
> 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


Edward Wijaya

2004-05-20, 1:32 pm



> 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
A. Sinan Unur

2004-05-20, 2:31 pm

"Edward Wijaya" <ewijaya@singnet.com.sg> wrote in
news:opr8blossspncm2o@news.singnet.com.sg:

> 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:

"Edward Wijaya" <ewijaya@singnet.com.sg> wrote in
news:opr8brpiqkpncm2o@news.singnet.com.sg:

> 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?

--
A. Sinan Unur
1usa@llenroc.ude (reverse each component for email address)
Paul Lalli

2004-05-20, 2:31 pm

On Fri, 21 May 2004, Edward Wijaya wrote:

> 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
Jürgen Exner

2004-05-20, 9:31 pm

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


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


>
> 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


Edward Wijaya

2004-05-20, 10:31 pm


> 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

Kevin Collins

2004-05-21, 12:31 am

In article <opr8cgn7edpncm2o@news.singnet.com.sg>, Edward Wijaya wrote:
>
>
> 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
Sponsored Links







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

Copyright 2008 codecomments.com