Nik has asked for the wisdom of the Perl Monks concerning the following question:

ok here i go i have the following code!
@files = <../data/texts/*.txt>; print start_form(-action=>"index.pl"); print p( {-align=>'center'}, font( {-size=>5, -color=>'Lime'}, 'Λόγ +ος Ψυχωφελής και Θαυμάσιος ---> ' ), popup_menu( -name=>'choice', -values=> +\@files ), submit('ok')); print end_form(), br(); if ( $file = param("choice") ) { open(FH, "$file") or die $!; @data = <FH>; close(FH); $data = join("", @data); }
I want the arrray @files = <../data/texts/*.txt>; who gives filanes like this ../data/texts/file1.txt to conever its values to file, file2,file3 and so on. to remove the path and the .ext that is.

Why? because i want to display the arrays values to a popup menu without the path and the extension.

Then before this line open(FH, "$file") or die $!; o want the array to be re-converted to its origianl state so that the file could actually open. This rquires s/// operator in my opinion but i dont knwo how to write it. Thanks!
The Devil Is In The Details!

Replies are listed 'Best First'.
Re: Array values reformation
by LassiLantar (Monk) on Jul 24, 2004 at 19:57 UTC
    Try map. "Map EXPR, LIST. This function evaluates the EXPR for each element of LIST (locally setting $_ to each element) and returns the list comprising the results of each such evaluation." (Programming Perl, p.740)
    @files = <../data/texts/*.txt>; @display_names = map( /([^\/]+)\.txt/, @files );
    Update: Oops! Didn't escape my "."...

    Now you can use @files if you want the full filename and @display_names if you want to put just the non-extension, non-pathname filenames into a popup menu.

    Peace,
    LassiLantar

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Array values reformation
by blue_cowdawg (Monsignor) on Jul 25, 2004 at 01:46 UTC

        I want the arrray @files = <../data/texts/*.txt>; who gives filanes like this ../data/texts/file1.txt to conever its values to file, file2,file3 and so on. to remove the path and the .ext that is.

    Check out File::Basename. Works like a charm.

    UPDATE: here's a program example:

    #!/usr/bin/perl -w use File::Basename; use strict; my @flist=<DATA>; chomp @flist; @flist = map { $_ = basename($_, qw/ .arb / ); } @flist; printf "%s\n",join(",",@flist); __DATA__ /home/pberghol/tmp/TestV1.arb /home/pberghol/tmp/TestV2.arb /home/pberghol/tmp/TestV3.arb /home/pberghol/tmp/TestV4.arb /home/pberghol/tmp/TestV5.arb /home/pberghol/tmp/TestV6.arb /home/pberghol/tmp/TestV7.arb /home/pberghol/tmp/TestV8.1.arb /home/pberghol/tmp/TestV8.2.arb /home/pberghol/tmp/TestV8.3.arb /home/pberghol/tmp/TestV8.4.arb /home/pberghol/tmp/TestV8.5.arb /home/pberghol/tmp/TestV8.arb
    and when it is run it produces:
    perl stripFnames.pl TestV1,TestV2,TestV3,TestV4,TestV5,TestV6,TestV7,TestV8.1,TestV8.2,Tes +tV8.3,TestV8.4,TestV8.5,TestV8

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Array values reformation
by ccn (Vicar) on Jul 24, 2004 at 19:55 UTC

    my @ary = map {/([^\/]+)\./; $1} @files;

    UPDATE: as Limbic~Region said ; $1 is not required here

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Array values reformation
by Joost (Canon) on Jul 26, 2004 at 13:54 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.