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

I have a txt file with this info where the delimeter is ";"
san francisco; oakland; new york city; boston; miami; detroit;
I want to count how many cities are listed so the above would have 6 as the count. My attempt below gives me a count of 1. Please advise how I can do it correctly??
my $filename = 'myname.txt'; my $ct = 0; open FILE, $filename || "$!"; while(<FILE>) { chomp; my @arr = split /;/, $_; $ct++ } print $ct; close FILE;

Replies are listed 'Best First'.
Re: Count of split data
by petdance (Parson) on Jan 12, 2007 at 15:15 UTC
    You're only incrementing by a count of 1 with $ct++, but what you want is to increment it by the number of elements in @arr. Do that like so:
    $ct = $ct + scalar(@arr); # or $ct += @arr;

    xoxo,
    Andy

Re: Count of split data
by brian_d_foy (Abbot) on Jan 12, 2007 at 15:37 UTC

    If you just want to count things you don't need to split the data. Use the transliteration operator (see perlop to replace the semicolons with itself. It returns the count of the number of things it replaced:

    my $count = $string =~ tr/;/;/

    Sometimes you'll see this without the replacement side, but it does the same thing.

    my $count = $string =~ tr/;//

    If you apply it to $_, it's a bit shorter since the binding operator disappears:

    # my $count = $_ =~ tr/;// # same thing my $count = tr/;//

    Now, if you want to do this for a bunch of lines and acculumate the count, just add to what you already have.

    my $count = 0; while( <$fh> ) { $count += tr/;// }

    If you really want the split to get out the city names though, it's almost the same idea:

    my $count = 0; while( <$fh> ) { $count += my @cities = split /;/; }
    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
Re: Count of split data
by davorg (Chancellor) on Jan 12, 2007 at 15:16 UTC
    $ct++

    You're simply adding one each time round the loop. I think you should be adding the number of elements in the array @arr.

    $ct += @arr;
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Thanks for such quick responses!!

      It works and I think perlmonks forum is the best technology forum (compared to the hundreds of other forums) because of the quick responses and expertise.
Re: Count of split data
by eff_i_g (Curate) on Jan 12, 2007 at 15:42 UTC
    You should not use || because the statement will never fail if $filename is true, i.e., you're not checking the opening of $filename, just $filename. The lower precedence or is the best fit, unless you add parentheses:

    open FILE, $filename or die $!;

    Also, see the docs about indirect filehandles and 3-argument open.
Re: Count of split data
by Joost (Canon) on Jan 12, 2007 at 15:16 UTC
Re: Count of split data
by jwkrahn (Abbot) on Jan 12, 2007 at 15:43 UTC
    open FILE, $filename || "$!";
    Shouldn't that be:
    open FILE, $filename or die $!;
    Or do you actually have a valid file name stored in $!?

    It looks like you want to count the semicolons:

    while ( <FILE> ) { $ct += tr/;//; } print $ct;