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

I have a text file that contains variables that looks like this:

255.255.255.255 domain.com smtp.domain.com user name <user\@domain.com> sendto <sendto\@sendto.domain.com></p? I use:

open (VARIABLES, "<variables.dat"); @Variables=<VARIABLES>; close VARIABLES;

to read the variables into an array. However, when I try and take user name <user\@domain.com> which is $Variable3 to send an email, only user name is recognised. I am losing <user\@domain.com>. What is wrong here?

Should I be using placeholders? Is there a better way to read variables with special characters into an array? Are the variables being loaded properly, but I need to handle them differently in the script?

I did not think that < and > were special charaters, and adding a \ infront fo them does not help my script. Any thoughts on which way I should direct my learnings?

Please advise,
Simon

Replies are listed 'Best First'.
Re: reading scalars from text file
by perlplexer (Hermit) on May 16, 2002 at 14:32 UTC
    Placeholders have nothing to do with files. You had a DBI nightmare, apparently.

    I'm not entirely clear on what you're trying to do here.
    Do you want to get "user" from each line in that file?
    If so
    open (VARIABLES, "<variables.dat") or die "Error : $!\n"; while (<VARIABLES>){ my ($ip, $domain, $smtp, $user, $name, $sendto) = split; print "$user\n"; } close VARIABLES;
    --perlplexer
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: reading scalars from text file
by mrbbking (Hermit) on May 16, 2002 at 16:38 UTC
    Your @Variables is an array, each element of which is a line in the file. That's what the diamond operator gives you in list context.

    So, $Variables[3] is the whole fourth line from the file called 'variables.dat'. That's not what you want. You're looking for the fourth *field* in whichever line. (I'm assuming those are tab-separated valued, BTW...)

    Without resorting to DBD::CSV or Text::CSV_XS (which may or may not be the best idea in your case), you can do it like this, after the code you posted in the root node:

    foreach ( @Variables ){ my $user_id = ''; my @elements = split /\t/; my $user_name = $elements[3]; # do something wonderful with $user_name; }
Re: reading scalars from text file
by broquaint (Abbot) on May 16, 2002 at 14:33 UTC
    If you want your variables in an array then you'll have to split the line e.g
    ## this will split on whitespace, and put the fields into @variables my @variables = split /\s+/, <VARIABLES>;
    If you're using flat files as storage facilities then you may want to look into the likes of Text::CSV, AnyData or perhaps even Inine::Awk.
    HTH

    _________
    broquaint