Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Creating a file in perl where part of the filename is an array element

Options
  • 19-10-2007 9:41am
    #1
    Registered Users Posts: 755 ✭✭✭


    I'm new to perl and am a bit stuck. Forgive me if the terminology used below isn't 100% correct.

    I have an input file with 5 fields separated by tabs. The third field in the file is the part I want to use, so I think that an array if the best way to reference this field.

    Is there any way of creating a file where part of the file name is an array element referring to the field above. The part of the script I'm stuck on is below:

    my $VP_Name;
    my @stats_output;

    open (StatsFile, "/export/home/admin/stats_files/stats_output") || die "Could not find file";

    while ($VP_Name = <StatsFile>) {
    @stats_output = split(/\t/, $VP_Name);

    open (FH, ">>/export/home/admin/stats_files/@stats_output[2]&quot;) || die "Could not create file";

    There is more before and after the script that hasn't any impact on what I'm trying to do here. I've a feeling that its something simple like punctuation.

    Thanks.


Comments

  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    Long time out of perl but could it be:

    open (FH, ">>/export/home/admin/stats_files/".@stats_output[2]) || die "Could not create file";

    I'm concatenating the variable to the rest of the file path in case normal interporlation of double-quoted strings doesn't work for array element references, can't recall, could be totally wrong here.

    Alternatively
    $stats_output = split(/\t/, $VP_Name)[2];
    Looks a bit mental but perl just may be powerful enough to deduce the scalar context,

    In any event the dogmatic approach:

    @stats_output = split(/\t/, $VP_Name);
    $stats_output = @stats_output[2];
    open (FH, ">>/export/home/admin/stats_files/$stats_output") || die "Could not create file";


    I presume it's not a permissions thing, that you've already created files in that directory...


  • Registered Users Posts: 755 ✭✭✭mr kr0nik


    Thanks for the help,

    I've created the directory but not the files as there are lots of them. I had thought that the open command creates the file if it doesn't exist.

    I'll give each of your suggestions a go and let you know how I get on.

    Cheers


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    mr kr0nik wrote: »
    while ($VP_Name = <StatsFile>) {
    @stats_output = split(/\t/, $VP_Name);

    open (FH, ">>/export/home/admin/stats_files/@stats_output[2]&quot;) || die "Could not create file";
    You access list elements with $listname[$number].
    open (FH, ">>/export/home/admin/stats_files/$stats_output[2]") || die "Could not create file";
    
    See perlinto or perldoc perldata.


Advertisement