Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

appending extension to filename

Posted on 2002-04-11
9
277 Views
Last Modified: 2010-03-05
Is there a way to do this:

        chomp($out_file = <>);
        $out_file = "${out_file}.csv";

in one line?
0
Comment
Question by:frenomulax
  • 4
  • 3
  • 2
9 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 6935007
frenomulax,

".Is there a way to do this:.."

What exactly are you trying to do?

Please explain in plain english, without posting any code.
0
 

Author Comment

by:frenomulax
ID: 6935043
I have a user enter a filename w/out an extension. I change the file name to include a .csv extension, so if they enter 'data_1' it becomes data_1.csv.

The above works fine, I just was wondering if there's a more concise way to do it.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6935068
frenomulax,

"..so if they enter 'data_1' it becomes data_1.csv. ..."

Try this....

NOTE: Run this program from the command line & let me know what output you can see.

$fn=<STDIN>;

$fn=~ s/\s+$/\.csv/;

print $fn;
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:frenomulax
ID: 6935140
When I run it as a stand alone script it works fine, I get filename.csv printed out. But essentially this is the same as my original code, since there is one line to read from stdin and one line to cat the extension.

I was wondering if there is a way to do it all on one line, reading from stdin, chomp the \n, and cat the extension to the filename.

BTW, this is just for my curiosity, as what I have is working well. No need to spend a lot of your time on it.
0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 6935193
($out_file = <>) =~ s/\n?$/.csv/;
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6935198
frenomulax,

"..g if there is a way to do it all on one line.."

then i think this is what you are looking for.....

$fn=eval($fn=<STDIN>).'.csv';

print $fn;
0
 

Author Comment

by:frenomulax
ID: 6935365
maneshr,
$out_file = eval($out_file = <STDIN>).'.csv';
returns the error Bareword "out" not allowed while "strict subs" in use, although when I comment out 'use strict;' it does work fine.

ozo, your solution works as is, I'll call that the answer.
0
 

Author Comment

by:frenomulax
ID: 6935389
one quick question for ozo, can you explain the meaning of the regexp you supplied? I see that it is replacing the first occourance of a new line with '.csv' but what does the ?$ represent ?

I'm slowly getting an understanding of these things. It is literally like learning a new language.

Thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 6935610
? makes the \n optional,and $ anchors it to the end of the string
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question