AARoads Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Thanks to everyone for the feedback on what errors you encountered at https://www.aaroads.com/forum/index.php?topic=33904.0
Corrected several already and appreciate your patience as we work through the rest.

Author Topic: Script to convert a CSV to a forum BBCode table  (Read 1234 times)

Scott5114

  • *
  • *
  • Offline Offline

  • Posts: 19715
  • Nit picker of unprecedented pedantry

  • Age: 34
  • Location: Las Vegas, NV
  • Last Login: Today at 08:11:04 AM
    • Denexa 100% Plastic Playing Cards
Script to convert a CSV to a forum BBCode table
« on: January 29, 2023, 09:06:12 PM »

Who knows if anyone else but me will ever find this useful, but, hey, if you do, knock yourself out.

No guarantees this does anything right. Licensed under whatever the latest GPL is whenever you read this.

Code: [Select]
#!/usr/bin/perl

use Text::CSV;
use strict;

# CSV parse object
my $csv = Text::CSV->new ({
  binary    => 1,
  auto_diag => 1,
  sep_char  => ','    # not really needed as this is the default
});

my $file = $ARGV[0] or die "No CSV file specified\n"; # Path to CSV file
open(my $data, "<", $file) or die "Could not open '$file' $!\n"; # Open file

print "[table]\n";

# Read file in line by line.
while (my $line = <$data>)
{
    print "[tr]";
   
    chomp $line;
   
    if ($csv->parse($line))
    {
        my @fields = $csv->fields();
       
        foreach(@fields)
        {
            print "[td]";
            print $_;
            print "[/td]";
        }
    }
    print "[/tr]\n";
}

print "[/table]\n";
Logged
uncontrollable freak sardine salad chef

kurumi

  • *
  • Offline Offline

  • Posts: 2572
  • Location: Cupertino, CA
  • Last Login: February 23, 2024, 11:02:56 PM
    • kurumi.com
Re: Script to convert a CSV to a forum BBCode table
« Reply #1 on: January 29, 2023, 11:25:10 PM »

It's good to see well-written Perl in the wild. Bookmarked.
Logged
My first SF/horror short story collection is available: "Young Man, Open Your Winter Eye"

Scott5114

  • *
  • *
  • Offline Offline

  • Posts: 19715
  • Nit picker of unprecedented pedantry

  • Age: 34
  • Location: Las Vegas, NV
  • Last Login: Today at 08:11:04 AM
    • Denexa 100% Plastic Playing Cards
Re: Script to convert a CSV to a forum BBCode table
« Reply #2 on: January 29, 2023, 11:45:00 PM »

Calling my code well-written warms my heart. Thanks. :)
Logged
uncontrollable freak sardine salad chef

 


Opinions expressed here on belong solely to the poster and do not represent or reflect the opinions or beliefs of AARoads, its creators and/or associates.