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

well I'm troubled with a problem.
Seems I don't catch what the problem exactly is?

#!/usr/bin/perl -w use strict; my $quotes = "/home/gtheys/quotes"; open(FILE,"<$quotes"); while (<FILE>) { chomp; my ($quote,$person) = split /--/; $quote =~ s/^\s+|\s+$//g; $person =~ s/^\s+|\s+$//g; print "$quote\t$person\n"; } close FILE;
But the problem is that in the textfile I open there's not always a -- to split because there's no name.
So what i did is split it in two files and put the lines with -- in another file. Then I parsed them seperatly.
This looks to me a route around the problem. While there must be a better solution I can't grasp at the moment.

--
My opinions may have changed,
but not the fact that I am right

Replies are listed 'Best First'.
Re: use strict and -w
by davorg (Chancellor) on Mar 15, 2001 at 14:54 UTC

    Why not check if the '--' exists before doing the split?

    #!/usr/bin/perl -w use strict; my $quotes = '/home/gtheys/quotes'; open(FILE, "<$quotes") || die "Badness: $quotes [$!]\n"; while (<FILE>) { chomp; my ($quote, $person); if (/--/) { ($quote, $person) = split /--/; } else { ($quote, $person) = ($_, ''); } $quote =~ s/^\s+|\s+$//g; $person =~ s/^\s+|\s+$//g; print "$quote\t$person\n"; } close FILE || die "Badness: $quotes [$!]\n";
    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

      Thanx for pointing out the or die while opening and closing files. Didn't type it 'cos it wasn't the rpoblem :) But nonetheless it needs to be done...

      --
      My opinions may have changed,
      but not the fact that I am right

Re: use strict and -w
by Jouke (Curate) on Mar 15, 2001 at 14:56 UTC
    I guess your problem is that if there's no $person, you get a warning of use of uninitialized value.
    You could get around this by only performing the action if the variable is defined like this:
    my ($quote,$person) = split /--/; $quote =~ s/^\s+|\s+$//g if defined $quote; $person =~ s/^\s+|\s+$//g if defined $person;
    hth,

    Jouke Visser, Perl 'Adept'
      and now totally error free ... Thanx to jouke:
      #!/usr/bin/perl -w use strict; my $quotes = "/home/gtheys/quotes"; open(FILE,"<$quotes") || die "Can't open quotes file: $!"; while (<FILE>) { chomp; my ($quote,$person) = split /--/; $quote =~ s/^\s+|\s+$//g if defined $quote; $person =~ s/^\s+|\s+$//g if defined $person; if (defined $quote) {print "$quote\t"}; if (defined $person) { print "$person";} print "\n"; } close FILE || die "Can't close quotes file: $!";


      --
      My opinions may have changed,
      but not the fact that I am right

Re: use strict and -w
by busunsl (Vicar) on Mar 15, 2001 at 14:45 UTC
    You could add a '-- ' to $_ and split into two fields, that would give you a blank name, if none exists:
    while (<FILE>) { chomp; $_ .= '-- '; my ($quote,$person) = split /--/, $_, 2; $quote =~ s/^\s+|\s+$//g; $person =~ s/^\s+|\s+$//g; print "$quote\t$person\n"; }
Re: use strict and -w
by merlyn (Sage) on Mar 15, 2001 at 19:58 UTC
    Lemme see. If there's no "--", there's no person. So write the code that way:
    while (<FILE>) { # no need for chomp, because you strip whitespace below if (my($quote, $person) = /(.*?)--(.*?)/s) { s/^\s+//, s/\s+$/ for $quote, $person; print "$quote\t$person\n"; } else { # no --, so just show the cleaned up quote: s/^\s+//, s/\s+$/; print "$_\t(Unknown)\n"; } }

    -- Randal L. Schwartz, Perl hacker

      Or, instead of grabbing what you want, just get rid of (or substitute) what you don't want (don't neglect the space after (Unknown) if you do want such an attribution):

      while(<FILE>){ $_ .= '-- (Unknown) ' unless /--/; s/^\s+//; s/\s+$/\n/; s/\s*--\s*/\t/; print; }
Re: use strict and -w
by extremely (Priest) on Mar 16, 2001 at 02:35 UTC
    Since this is turning into a TIMTOWTDI type discussion, why not generalize it:
    while (<FILE>) { chomp; my @f = split /--/; s/^\s+|\s+$//g for @f; local $"="\t"; print "@f\n"; }

    --
    $you = new YOU;
    honk() if $you->love(perl)