Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

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

  • 19-10-2007 08:41AM
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 6,704 ✭✭✭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