Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default formats

    Is it possible to use more than one format in a script? I am parsing a log file to STDOUT and would like to be able to write to two (or more) different formats depending on the information found in the log.

    This is what I'm trying to do:

    1. open and read logfile
    2. if you find "some_string" print to STDOUT using TYPE_1 format
    3. if you find "other_string" print to STDOUT using TYPE_2 format
    5. continue parsing logfile, etc

    I can open and parse the logfile, my script breaks whenever I try to call more than one type of format. This doesn't seem to work for me, is this even possible?


    format TYPE_1 =
    Type: @<<<<<<<<<< Gateway: @<<<<<<<<< Acct ID: @<<<<<<<<<<<<<<<<<<
    $record_type,$gateway,$acct_id
    Start Date: @<<<<<<<<<< Start Time: @<<<<<<<<<
    $start_date,$start_time
    ..
    write TYPE_1;

    format TYPE_2 =
    Type: @<<<<<<<<<< Gateway: @<<<<<<<<< Acct ID: @<<<<<<<<<<<<<<<<<<
    $record_type,$gateway,$acct_id
    Start Date: @<<<<<<<<<< Start Time: @<<<<<<<<<
    $start_date,$start_time
    Disconnect Date: @<<<<<<<<<< Disconnect Time: @<<<<<<<<<
    $disco_date,$disco_time
    ..
    write TYPE_2;


    Also, how would I go about keeping the filehandles open as it would be repetively "writing" each format.


    Thank you,
    Jose

    Jose Malacara Guest

  2. Similar Questions and Discussions

    1. Play various formats
      I want to make an application in which user can play any video files from its own. Either file is in .wmv format or .mpeg4, Flash playes it. Is it...
    2. save as formats
      I have tried to save a scanned pdf to word using the save as feature. The entire three page typed document is treated as one image. I am using Adobbe...
    3. PNG Formats in Vista
      Try this, see if this works. On Windows XP (with Classic theme): 1) Type in at Run, "winver" then press OK. 2) Press Alt+Printscreen when About...
    4. file formats?
      hi, i am new to photoshop 7, so please bear with me, i have downloaded certain pictures off the net and i am trying to resize and then print them,...
  3. #2

    Default RE: formats

    To just use it for STDOUT, it's pretty straight forward, switching the
    special $~ variable as needed.

    You're switching FORMATs not filehandles. There may be a shortcut but this
    is how I would do it for this problem:

    -Tom Kinzer

    _____________________

    format TYPE_1 =
    Im formated with type1: @<<<<<<<<< @||||||||||||||||||||||||||||||||||||||
    $field1, $field2
    ..

    format TYPE_2 =
    IM FORMATTED WITH TYPE2: @<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<
    $field1, $field2
    ..

    open RECORDS, "< YourInput.log" or die;

    while (<RECORDS>) {

    ($field1, $field2) = split;

    if ( m/YourPatternRegExHere/ ) {

    $~ = 'TYPE_1';

    } else {

    $~ = 'TYPE_2';

    }

    write;

    }
    close RECORDS;

    __END__

    -----Original Message-----
    From: Jose Malacara [mailto:josem@thrifty.net]
    Sent: Friday, December 05, 2003 6:40 PM
    To: [email]beginners@perl.org[/email]
    Subject: formats


    Is it possible to use more than one format in a script? I am parsing a log
    file to STDOUT and would like to be able to write to two (or more) different
    formats depending on the information found in the log.

    This is what I'm trying to do:

    1. open and read logfile
    2. if you find "some_string" print to STDOUT using TYPE_1 format
    3. if you find "other_string" print to STDOUT using TYPE_2 format
    5. continue parsing logfile, etc

    I can open and parse the logfile, my script breaks whenever I try to call
    more than one type of format. This doesn't seem to work for me, is this even
    possible?


    format TYPE_1 =
    Type: @<<<<<<<<<< Gateway: @<<<<<<<<< Acct ID: @<<<<<<<<<<<<<<<<<<
    $record_type,$gateway,$acct_id
    Start Date: @<<<<<<<<<< Start Time: @<<<<<<<<<
    $start_date,$start_time
    ..
    write TYPE_1;

    format TYPE_2 =
    Type: @<<<<<<<<<< Gateway: @<<<<<<<<< Acct ID: @<<<<<<<<<<<<<<<<<<
    $record_type,$gateway,$acct_id
    Start Date: @<<<<<<<<<< Start Time: @<<<<<<<<<
    $start_date,$start_time
    Disconnect Date: @<<<<<<<<<< Disconnect Time: @<<<<<<<<<
    $disco_date,$disco_time
    ..
    write TYPE_2;


    Also, how would I go about keeping the filehandles open as it would be
    repetively "writing" each format.


    Thank you,
    Jose

    Tom Kinzer Guest

  4. #3

    Default Re: formats


    On Dec 5, 2003, at 6:39 PM, Jose Malacara wrote:
    [..]
    > write TYPE_1;
    that's the sticky bit, where tom is
    talking about not writing to a filehandle...

    [..]

    To expand on tom's comments a bit:
    <http://www.wetware.com/drieux/pbl/perlTrick/Format/funk_the_form.plx>

    which generates

    Type: type1 Gateway: cow Acct ID: lost_id
    Start Date: 2003.12.2 Start Time: 1400
    #---------------
    Type: type2 Gateway: pig Acct ID: new
    Start Date: 2003.12.2 Start Time: 1400
    Disconnect Date: 2003.12.3 Disconnect Time: 1600


    one needs to be thinking in terms of

    $~ = 'TYPE_1';
    write;
    $~ = '';
    print "#---------------\n";

    HTH.


    ciao
    drieux

    ---

    Drieux Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139