Trouble getting data from a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • g0uki
    New Member
    • Feb 2008
    • 5

    Trouble getting data from a file

    Hi all,

    i hope you can help, I'm having some trouble getting all the data from a file, well just the date and time entries. I'll be doing operations on the data so would like to store it within variables.

    My script's output is looking a bit strange...

    The file is sent from a server so changing it will be difficult. Nearly all of the data are in quotes except the date and time.

    First line of the file are the headings, then the data:
    "Car ID" "Descriptio n" "Status" "Condition" "Time arrived" "Location" "car_counrt y"
    "ABC123" "Lamborghin i Countach" "used" "good" 17/02/2006 15:00:00 "alpha" "Italy"

    My script:
    [code=perl]
    #!/usr/bin/perl -w
    use strict;

    print "enter filename\n";
    chomp(my $filename = <>);

    open(FILE, $filename) || die "can't open $!\n";

    while (<FILE>){
    if (m/ABC/){
    my ($ID, $description, $status, $condition, $time_arr, $location, $country) = split/(\"\w+\")/;
    print "ID:$ID\ndescri ption: $description\ns tatus: $status\ncondit ion: $condition\narr vied: $time_arr\nloca tion: $location\ncoun try: $country\n\n";
    }
    }
    [/code]


    and the output:
    ID:
    description: "ABC123"
    status: "Lamborghin i Countach"
    condition: "used"
    arrvied:
    location: "good"
    country: 17/02/2006 15:00:00

    How would i get the date and time? Any assistance would be greatly apprieciated.

    i know the issue lies on the 11th line, but this has annoyed me for a bit now :(

    thanks for yuor time
    g0uki
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    This suggestion is very dependent on the file being formatted exactly how you showed and that the script does not have to validate lines for proper formatting:

    [code=perl]
    while (<FILE>){
    if (m/ABC/){
    my ($ID, $description, $status, $condition, $time_arr, $location, $country) = split/"\s+"|"\s+| \s+"/;
    $ID =~ s/^"//;
    print "ID:$ID\ndescri ption: $description\ns tatus: $status\ncondit ion: $condition\narr vied: $time_arr\nloca tion: $location\ncoun try: $country\n\n";
    }
    }
    [/code]

    Comment

    • g0uki
      New Member
      • Feb 2008
      • 5

      #3
      Nice!
      Thanks KevinADC :)

      I dont understand how you've made it work though?

      the line: split/"\s+"|"\s+| \s+"/;

      splits the line by whitespace, but 2 times?

      "\s+|\s+"/; -> this tests for an 'or' option?

      Would it be okay if its explained? i really want to get to grasp with perl's regex.

      Thanks again
      g0uki

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        the '|' used in that context literally means "or" (called alternation)

        split/"\s+" or "\s+ or \s+"/;

        first it tries to split on 'comma space(s) comma' : "\s+"
        then it tries to split on 'comma space(s)' : "\s+
        and last it tries to split on 'space(s) comma ': \s+"

        those are the three patterns in the lines that will nearly result in the fields of data you want, all that remains to do is remove the leading comma from the first field which is what the next line does:

        $ID =~ s/^"//;

        there are other ways to split those lines into fields of data, that was just the first way I saw that would work using split(). If the fields are fixed width you could use substr or unpack, but if they are not fixed width fields you can use split or other regexps.

        Comment

        • g0uki
          New Member
          • Feb 2008
          • 5

          #5
          Hi KevinADC,

          Sorry for my late reply and thanks for the explanation into this problem i was having.

          I like the way its been explained, but will admit that so far in my perl training i haven't come across regex alternation yet, but it looks very handy.

          thanks again for your help on this
          best regards
          g0uki

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            You're welcome. I noticed I kept saying "comma" in my explanation when I should have said "double-quote".

            Comment

            Working...