Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Unable to concatinate a Path to the array elements, which will have the values as return values from sunroutine

by perladdict (Chaplain)
on Feb 24, 2014 at 15:04 UTC ( #1075991=perlquestion: print w/ replies, xml ) Need Help??
perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I have a text file which have a one column listing of file names, the suroutine which is defined for reading the txt file to extract only 8 charecter reports name. Caling the subroutine and assigning it to array variable as in below code when i try to append a path "/efsprod/docmnt" to each file name from array element, it displaying filename and at end i could see the "/test/var/o" as output.
Text file</br> #AAA45S49.${datestamp}.${timestamp}.txt #BBB75SWI.${datestamp}.${timestamp}.txt #YYYYYY.${datestamp}.${timestamp}.txt #XXXXXXX.${datestamp}.${timestamp}.txt #CCCCCCC.${datestamp}.${timestamp}.txt #DDDDDDD.${datestamp}.${timestamp}.txt #EEEEEEE.${datestamp}.${timestamp}.txt #FFFFFFF.${datestamp}.${timestamp}.txt #FFFFFF2.${datestamp}.${timestamp}.txt #GGGGGGG.${datestamp}.${timestamp}.txt #HHHHHHH.${datestamp}.${timestamp}.txt #IIIIIII.${datestamp}.${timestamp}.txt #JJJJJJJ.${datestamp}.${timestamp}.txt
Code I am trying to append the filename to the Path as below
#!/usr/bin/perl @array = &report(); foreach $file(@array) { $path = "/efsprod/docmnt/"; $file = "$path" . "$file"; print "$file"; } sub report() { open(F,"Textfile.txt") or die "$!\n"; while(chomp($line=<F>)) { $line=substr($line,0,9)l $line= reverse($line); chop $line; $line=reverse($line); print "$line\n"; }
Once after succeeding the concatination i need to verify the each file which is existed in "/efsprod/docmnt", if exist i need to copy the file from source "/efsprod/docmnt" to destination directory as current directory from where i am running the script(i.e /tmp/xxx/). The above code just printing the filenames which has been red from the text file and at the end of the o/p displaying as /efsprod/docmnt/0
The o/p i am expecting as $file = /test/var/XXX001 /test/var/...... /test/var/XXX01B #but i am getting o/p as XX001BC YY001BC ...... ...... /test/var/0

Comment on Unable to concatinate a Path to the array elements, which will have the values as return values from sunroutine
Select or Download Code
Re: Unable to concatinate a Path to the array elements, which will have the values as return values from sunroutine
by hazylife (Monk) on Feb 24, 2014 at 15:25 UTC

    But your subroutine just prints the values instead of returning them, hence the (implicitly returned) "0". There are much more efficient ways of doing this, e.g.:

    perl -ple '$_ = "/efsprod/docmnt/" . substr $_, 1, 8' Textfile.txt
Re: Unable to concatenate a Path to the array elements, which will have the values as return values from subroutine
by Athanasius (Abbot) on Feb 24, 2014 at 15:51 UTC

    The line:

    while(chomp($line=<F>)) {

    has a bug. The chomp function does not return the string which has been chomped; it returns the number of characters removed. So, if the last line in the file does not end in a newline character, chomp returns zero, the while loop terminates, and the last line of data is lost. If the last line does end in a newline character, the last line is processed but the loop doesn’t terminate, so on the next iteration chomp is called on a value of undef, which results in a warning if use warnings; is in effect. The correct approach is:

    while ($line = <F>) { chomp $line; ...

    BTW, you do always begin your scripts with:

    use strict; use warnings;

    don’t you?

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Unable to concatinate a Path to the array elements, which will have the values as return values from sunroutine
by GotToBTru (Priest) on Feb 24, 2014 at 16:01 UTC

    Your subroutine never inserts anything into an array, so nothing is returned by report() and stored in @array. Also, while your reverse-chop-reverse may be good kung-fu, it's much easier to substr($line,1,8) to get rid of the '#' at the beginning. Further, the substr means you don't need the chomp.

    use strict; use warnings; my $path = "/efsprod/docmnt/"; my @array = &report(); foreach my $line (@array) { $line = $path . $line; print "$line\n"; } sub report() { open(F,"Textfile.txt") or die "$!\n"; my (@filelist); while(<F>) { push @filelist, substr($_,1,8); } return @filelist; }

    Update(s): add path to array elements, not just to print statement. Remembered after that foreach aliases placeholder with array elements so we don't have to replace with C-style for.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1075991]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2015-07-06 02:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (69 votes), past polls