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

Hi All,

I'm new to Perl, and to PM so gentle guidance is welcome! Having said that I do have many years on computers.

I am attepting to write a Perl script to retrieve an email from a pop3 mailserver. The email contains only one line of text that is comprised of 6 tab delimited data fields so I need to parse the 6 data values to a mysql data base. I've been able to connect to the server and download the email, and I know enough mysql to make that work .... but I can't do the simplest part in the middle, which is extract the 6 individual data values from the text! I'm willing to work at it, have googled and have good books, but still cna not figure out how to use regexp search and then assign variable names to the data values. Initially I'm just trying to print the variables to see if I got them.

Thanks for all suggestions.

Jim

=================
data file

1234567890123456 2004-07-10 14:47:39 51.12345 -1.12345
=================

my latest failing perl script!

#!/usr/bin/perl open (INPUT, "< data") or die "Couldn't open data file for reading: $!\n"; while (<INPUT>) { $firstvalue =~ /(\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d)/; # match first 16 + digit value print $firstvalue #"This is the first parameter.\n"; } close (INPUT);
text
jamaas btinternet com

janitored by ybiC: Balanced <code> tags around codeblock, as per Monastery convention.

Replies are listed 'Best First'.
Re: newbie attempting to extract
by Velaki (Chaplain) on Oct 03, 2004 at 07:17 UTC

    I believe that you are looking for the split function.

    #!/usr/bin/perl use strict; use warnings; while(<DATA>) { my @a = split; # split record into fields print "Value = $_\n" for @a; # print out all fields print "\n"; # blank line between "records" } __DATA__ 1234567890123456 2004-07-10 14:47:39 51.12345 -1.12345
    That will produce
    Value = 1234567890123456 Value = 2004-07-10 Value = 14:47:39 Value = 51.12345 Value = -1.12345

    Hope that helped,
    -v
    "Perl. There is no substitute."
      It helped a whole bunch!! And so elegant.
      OK, next challenge!
      Thanks and Regards
      Jim
      jamaas btinternet com
      Thanks Velaki,


      Obviously this works beautifully. I've tried reading the perldoc info on split.... and tried to put in an incrementing loop to give the values unique names such that these can then be passed to php code for insertion into mysql. It appears that split does all this in one pass so incrementing a loop counter doesn't work. Can this @_array be used to directly insert into mysql table?


      Thanks a bunch

      Jim
      jamaas btinternet com

        This provides a unique value for each data item. It combines a row ID ($.) and a field ID ($i).

        # data_read use strict; use warnings; while (<DATA>) { chomp; my @data = split; for (my $i = 0; $i <= $#data; ++$i) { print "$._$i = $data[$i]\n"; } } __DATA__ qwerty qwerty qwerty asdf zxcv 1234 qwerty qwerty qwerty asdf zxcv

        Here's the output:

        [ ~/tmp ] $ perl data_read 1_0 = qwerty 1_1 = qwerty 1_2 = qwerty 1_3 = asdf 1_4 = zxcv 1_5 = 1234 2_0 = qwerty 2_1 = qwerty 2_2 = qwerty 2_3 = asdf 2_4 = zxcv [ ~/tmp ] $

        Here's a perl.com article which describes $. and other special variables.

        Regards,

        PN5

Re: newbie attempting to extract
by CountZero (Bishop) on Oct 03, 2004 at 12:09 UTC
    Also have a look at the Text::CSV_XS or Text::CSV modules.

    They also deal with (un)quoting "forbidden" characters in your CSV-records.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: newbie attempting to extract
by Cody Pendant (Prior) on Oct 04, 2004 at 05:46 UTC
    If I understand the question right, the simplest solution to it is surely just this kind of syntax:
    $str = 'one two three'; ($x,$y,$z) = split(' ',$str); print "my three variables are $x, $y and $z";
    as in, it directly splits up the string into variable names of your choosing?

    Maybe I've missed something, as nobody's posted about that method yet?



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss')
    =~y~b-v~a-z~s; print