Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Summary of Problem: You are creating a dynamic array instead of contatenating strings.

Simply to get it to run, I adjusted your code as follows:

#!/usr/bin/perl -w use strict; my $filename = <>; chomp $filename; my @filenam = split ('\.', $filename); my $fi; my $fff=($filenam[0],"_header",".txt"); open($fi,">", $fff) or die "Open failed for $fff: $!"; __END__
Which produced the following:
C:\Steve\Dev\PerlMonks\P-2013-07-17@0950-Filename-Transmogrification>p +erl filenameTransmogrification.pl Useless use of array element in void context at filenameTransmogrifica +tion.pl line 9. Useless use of a constant in void context at filenameTransmogrificatio +n.pl line 9. abc.dat C:\Steve\Dev\PerlMonks\P-2013-07-17@0950-Filename-Transmogrification>d +ir Volume in drive C has no label. Volume Serial Number is E88F-F456 Directory of C:\Steve\Dev\PerlMonks\P-2013-07-17@0950-Filename-Transm +ogrification 07/17/2013 09:52 AM <DIR> . 07/17/2013 09:52 AM <DIR> .. 07/17/2013 09:52 AM 0 .txt 07/17/2013 09:51 AM 232 filenameTransmogrification.pl 3 File(s) 466 bytes 2 Dir(s) 78,355,476,480 bytes free

Interestingly, I was expecting it to create 3.txt, but it merely created .txt. I will have to look into why; the clue is in the warning messages, I'm sure.

Anyway, I changed one line:

my $fff=($filenam[0],"_header",".txt"); to my $fff=$filenam[0] . "_header" . ".txt";
And got the following results:
C:\Steve\Dev\PerlMonks\P-2013-07-17@0950-Filename-Transmogrification>p +erl filenameTransmogrification2.pl abc.dat C:\Steve\Dev\PerlMonks\P-2013-07-17@0950-Filename-Transmogrification>d +ir Volume in drive C has no label. Volume Serial Number is E88F-F456 Directory of C:\Steve\Dev\PerlMonks\P-2013-07-17@0950-Filename-Transm +ogrification 07/17/2013 09:53 AM <DIR> . 07/17/2013 09:53 AM <DIR> .. 07/17/2013 09:52 AM 0 .txt 07/17/2013 09:53 AM 0 abc_header.txt 07/17/2013 09:51 AM 232 filenameTransmogrification.pl 07/17/2013 09:52 AM 234 filenameTransmogrification2.pl 4 File(s) 466 bytes 2 Dir(s) 78,354,427,904 bytes free
This seems to be closer to what you were trying to accomplish.

So, some observations:

1) Simple string contantenation in Perl is done using the  .  operator.

2) You might benefit from thinking in terms of using reciprocal functions. You used split to break apart the name; it would be prudent to use join to put it back together. split and join are essentially reciprocal functions.

3) There are modules which do a pretty good job of handling most filename related functions, and often honor the rules and trends of the OS you're running on, but this looks like a learn-Perl kind of exercise so there is much to be gained from learning how to do the basic work by hand. Just be aware that modules, especially ones published to CPAN, usually handle a lot of the corner cases you will overlook, so learning to leverage the publicly-shared work of others is a valuable skill in this profession.

Good luck!


In reply to Re: appending file name by marinersk
in thread appending file name by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (3)
    As of 2019-10-21 00:08 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?