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

Help. I decided to offer a free camera lens on my youtube channel. When I decided to do so, I did not realize that youtube does not have a tool for exporting the subscribers. You need to be subscribed to be able to enter. So I will have to manually capture all records. They look like this

Eli Stern,,,
10-Nov-19,,,
1 subscriber,,,
,,,
Abdessamed Ham,,,
15-Nov-19,,,
0 subscribers,,,
,,,
Arne Martinson,,,
22-Nov-19,,,
0 subscribers,,,
,,,

I need to reach, scrub and output to look like this:

1,Eli Stern, 10-Nov-19,1 subscriber,,
2,Abdessamed Ham,15-Nov-19,0 subscribers,,
3,Arne Martinson,22-Nov-19,0 subscribers,,

I'd greatly appreciate your help. I used to programme years ago, but after two hours I am just producing garbage. My channel is Ordinary Filmmaker for this interested.

Replies are listed 'Best First'.
Re: 20 years confused
by tobyink (Canon) on Jun 13, 2020 at 08:01 UTC

    You're going to be out of luck. If you go to your YouTube account settings, and go to the Privacy section, you'll see this option:

    Keep all my subscriptions private

    If somebody has chosen this option, they can subscribe to you and they will not appear on your subscriber list. They're subscribed; they'll get your videos in their feed; your subscriber count will go up by one; but you will not be able to see that they are subscribed to you.

    I just checked the subscriber list of a very small YouTube channel that I have access to. The channel only has 44 subscribers. The list of subscribers I'm able to see only had 27 people, which implies 17 of them had this option enabled. That's 39% of them. Obviously this is a VERY small sample, but this isn't a channel that is especially technology-oriented or privacy-oriented, so I don't have any reason to think these numbers would be atypical.

    I don't think you thought through the terms and conditions for your competition, and I think that you're probably best off replacing your original entry requirements.

Re: 20 years confused
by kcott (Archbishop) on Jun 13, 2020 at 05:35 UTC

    G'day OrdinaryFIlmmaker,

    Welcome to the Monastery.

    It would've helped if you'd read "How do I post a question effectively?" before posting. Providing your data as paragraph (<p>) text does not allow us to see what it really is: HTML will, amongst other things, convert sequences of whitespace to a single space. We might have got clues from your code, but you didn't post that.

    Here's what your input data looked like (newlines are represented by '$'s):

    $ cat -vet pm_11118005_data Eli Stern,,, $ 10-Nov-19,,, $ 1 subscriber,,, $ ,,, $ Abdessamed Ham,,, $ 15-Nov-19,,, $ 0 subscribers,,, $ ,,, $ Arne Martinson,,, $ 22-Nov-19,,, $ 0 subscribers,,, $ ,,, $

    Your wanted output is also questionable: space before 1st date but not the others; and, records ending with 2 commas, a space, and a newline.

    Except for the dodgy space before the 1st date, this code produces the output you asked for:

    #!/usr/bin/env perl use strict; use warnings; use autodie; { open my $fh, '<', 'pm_11118005_data'; local $/ = "\n,,, \n"; while (<$fh>) { chomp; print join(',', $., split(/,,, \n?/)), ",, \n"; } }

    Output:

    1,Eli Stern,10-Nov-19,1 subscriber,, 2,Abdessamed Ham,15-Nov-19,0 subscribers,, 3,Arne Martinson,22-Nov-19,0 subscribers,,

    Update: A very minor update (adding a single missed letter) which changes the meaning (to what I originally intended).
    Was: "I would've helped if ..."
    Now: "It would've helped if ..."

    — Ken

      That is a good catch — select-and-paste trimmed the trailing whitespace on my system and I did not notice the "odd space out" in the sample output.

Re: 20 years confused
by jcb (Parson) on Jun 13, 2020 at 02:02 UTC

    I suspect an XY problem, but here is a 15-minute (5 minutes to write the script, 10 minutes to explain it) solution for the problem as stated:

    #!/usr/bin/perl use strict; use warnings; my @record = (); # record accumulator my $row = 1; # record counter while (<DATA>) { chomp; s/,,,$//; if (m/^$/) { print join(',', $row++, @record), ",,\n"; @record = (); # reset for next record } else { push @record, $_; } } __DATA__ Eli Stern,,, 10-Nov-19,,, 1 subscriber,,, ,,, Abdessamed Ham,,, 15-Nov-19,,, 0 subscribers,,, ,,, Arne Martinson,,, 22-Nov-19,,, 0 subscribers,,, ,,,

    For the simplest option, just paste your real data in after the __DATA__ line instead of your sample data and run the script. Note that you may need to add use utf8; to the script code if your data is in UTF-8, since that pragma will also set the encoding for the DATA filehandle, but encoding should not be a problem because the script treats its input as mostly-opaque byte strings.

    For a slightly more complex option, put all the input into a file, delete __DATA__ and everything after it, and change while (<DATA>) to while (<>), then run as "perl ./script.pl input.txt > output.txt".

    The script works by collecting fields (into the @record array) until it sees an empty line, then joining together an incrementing counter and the contents of the fields, printing that with a ",,\n" sequence to end the line, and clearing the record buffer to start accumulating the next record. The documentation for chomp and push are likely to be informative, as are the perlre and perlretut pages. This script uses the implicit assignment to $_ that the <> I/O read operator provides when used as a loop test and the magic DATA filehandle for reading the tail of the script file.