Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Split array

  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts

    Split array

    HI, I want to split an array but having problem doing it.
    Here is the array:

    Time: Wed Nov 03 21:55:23 2010 GMT

    I need to have:

    Time[ar0] Wed[ar1] Nov[ar2] 03[ar3] 21:55:23[ar4] 2010[ar5] GMT[ar6]

    Do you know the site I could find good examples on splitting an array with multiple delimiters?
    Thank you.

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    The answer is in your question.

    See: `perldoc -f split`
    http://perldoc.perl.org/functions/split.html

  • Users who have thanked FishMonger for this post:

    vl123456 (04-02-2011)

  • #3
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Good website I checked it already but I do not have the level of understanding of Perl to use it yet.
    It is like trying to read “Monks” site, there are a lot of familiar words but for those who speaks Monks only. I’m just a regular (beginner) guy.
    Thank you.

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    I'm not sure where you're at. Do you need more info from me, or are you working on the next step, which is to write/test some code?

    If you're not sure what to do, then you need to provide more info and ask a more specific and detailed question.

  • Users who have thanked FishMonger for this post:

    vl123456 (04-02-2011)

  • #5
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    I got a log file with the strings like this:
    Time: Wed Nov 03 21:55:23 2010 GMT

    I’m trying to loop the file and when the matched string found make it an array list so I would have 9 scalars. I want to replace the scalar Wed[1] with scalar 2010[7]. In the logfile the Wed/Nov/2010 are changing all the time.
    Here is the script I’m working on but not much success yet.
    #!/usr/bin/perl

    my$infile = "SM_Error.log";
    my$outfile = "mlogout.log";
    open (IN,"< $infile") or die "Can't open $filename : $!";
    open (OUT," >>$outfile") or die "Can't open $filename : $!";

    close IN ;
    close OUT ;

    open (OUT,">>$infile") or die "Can't open $filename : $!";
    while (<IN> )
    {
    if ($_ =~m/^Time:\s*[A-Z]+/gi)

    {
    @array = split (/:/, $_) ;
    #I'm stuck here cannot find delimiters

    @array1 = @array7
    }
    }
    close OUT ;

  • #6
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    your string is ike this?
    Code:
    Time: Wed Nov 03 21:55:23 2010 GMT
    $array[0] = Time:
    $array[1] = Wed
    $array[2] = Nov

    etc? yes?

    and you want to replace 'Wed' with 2010?

    I don't understand this bit...

    In the logfile the Wed/Nov/2010 are changing all the time.
    are those lines not different all the time because the time is different?
    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • Users who have thanked bazz for this post:

    vl123456 (04-02-2011)

  • #7
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Hmm,

    You first open 2 filehandles, one for reading and one for appending, then you immediately close both of those and then reopen the original 'infile' in append mode and try to read from that filehandle. Obviously that's not going to do what you expect.

    Add these 2 lines near the beginning of the script and try running it again then post the errors you received and a new question based on those errors.
    Code:
    use strict;
    use warnings FATALS => 'all';

  • Users who have thanked FishMonger for this post:

    vl123456 (04-02-2011)

  • #8
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Hi guys,
    Thanks for the replies,
    It might looks strange for you, you are the Pros here, I’m just trying to apply what I learned from >Google>.
    Yes, this line < Time: Wed Nov 03 21:55:23 2010 GMT> is the line from a log file and naturally the words <Wed> <Nov> and the year like <2010> are changed all the time depends on the day/month and the year it was created.
    I want to build reports and pull all the errors from the log files by <Time :> line but I want this line to look like this:
    < TIME: = YEAR then DAY then MONTH then H:M:S >
    I wanted to split the line and replace array <DAY> with the array <YEAR> .
    I used delimiter<:> but it did not work out as I wonted.

    Thanks again, vl123456

  • #9
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    try the delimiter on the space ' ' and you'll get the broken down parts you need, whilst retaining the time part intact. 21:55:23

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • Users who have thanked bazz for this post:

    vl123456 (04-02-2011)

  • #10
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    I'm lost, made so many changes, now it does not work at all.
    #!/usr/bin/perl

    my$infile = "SM_Error.log";
    my$outfile = "mlogout.log";
    open (IN,"< $infile") or die "Can't open $filename : $!";
    open (OUT," >>$outfile") or die "Can't open $filename : $!";

    close IN ;
    close OUT ;

    open (IN,"$infile") or die "Can't open $filename : $!";

    while (<IN> )
    {
    if ($_ =~m/^Time:\s*[A-Z]+/gi)

    {
    @array = split (/' '/, $_) ;

    print "@array" ;
    }
    }

  • #11
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    re-read post #7 from FishMonger about opening and closing your filehandles.

    here's one way to split your date string.

    Code:
    my $var = "Time: Wed Nov 03 21:55:23 2010 GMT";
     
     my @array = split / / , $var, 7;
     
     print qq( array1=$array[1] : array2=$array[2] : array5=$array[5] );
    Basically, you are trying to split a string ($var), and put each of its 'components' into a var of their own before re-organising it as you need.

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • Users who have thanked bazz for this post:

    vl123456 (04-05-2011)

  • #12
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    It's unclear to me what you're needing to accomplish.

    Do you simply need to reformat the date string?

    Instead of saying you want to "split an array", explain what you need to accomplish and let us show you how to do it. Please post a sample of the log file and how it should look after the processing.

  • Users who have thanked FishMonger for this post:

    vl123456 (04-05-2011)

  • #13
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Hi,
    Here is the part of the log I’m working on.
    +++++++++++++++++++++++++++++++++++++++
    Time: Mon Mar 14 22:29:00 2011 GMT
    ErrorCode: 0xC010023
    Message: Error '16777252' from the Machine '1': Exception caught from class/user method code. DLLinfo: LPars, Method: initialize().
    Time: Mon Mar 14 22:44:14 2011 GMT
    ErrorCode: 0xC010023
    Message: Error '16777252' from the Machine '1': Exception caught from class/user method code. DLLinfo: LPars, Method: initialize().
    Time: Mon Mar 14 22:45:21 2011 GMT
    ErrorCode: 0xA010023
    Message: Error '16777252' from the Machine '1': Exception caught from class/user method code. DLLinfo: LPars, Method: initialize().
    Time: Tue Mar 15 17:01:04 2011 GMT
    ErrorCode: 0x1010001
    Message: File D:\ClbxTDir\CT\BT\Cond_Files\TM250MHZ.Ready was not found
    Time: Tue Mar 15 17:01:04 2011 GMT
    ErrorCode: 0x1010001
    Message: File D:\ClbxTDir\CT\BT\Cond_Files\TM250MHZ.Ready was not found
    +++++++++++++++++++++++++++++++++++++++++++++++++++

    In the string:
    < Time: Wed Nov 03 21:55:23 2010 GMT>
    I’d like to replace the word <Wed> with the year <2010>
    I want the string to look like this:
    <Time: 2010 Nov 03 21:55:23 >
    I need to replace (swap) the <Day> with the <YEAR>. I know how to remove <GMT> and make the <Month> to <11> but still cannot replace (swap) <Day> to <YEAR>.
    Here is the code I got:
    my$infile = "SM_Error.log";
    my$outfile = "mlogout.log";
    open (IN,"< $infile") or die "Can't open $filename : $!";
    open (OUT," >>$outfile") or die "Can't open $filename : $!";

    close IN ;
    close OUT ;

    open (IN,"$infile") or die "Can't open $filename : $!";
    while (<IN> )
    {
    if ($_ =~/Time:/gi)
    {
    @array = split (/[:\s]/, $_) ;
    foreach my$date (@array)
    {
    print "$date\n" ;
    }
    }
    }
    close IN ;
    sleep 5 ;

  • #14
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    4
    Thanks
    0
    Thanked 4 Times in 4 Posts
    I'm not really sure why you're doing this:

    @array = split (/[:\s]/, $_)

    when this seems to accomplish what you want to do

    @array = split (/ /, $_)

    it splits your string into an array based on where the spaces are.

    Anyway, reordering day and year is pretty simple all you need to do is find the position of them in the array and swap them.
    ex)
    $temp = $array[6];
    $array[6] = $array[4];
    $array[4] = $temp;

    not the best way to do it but should hold you over until you get something better.

    Is that what you were asking?

  • Users who have thanked 4Rum for this post:

    vl123456 (04-06-2011)

  • #15
    New Coder
    Join Date
    Mar 2011
    Posts
    31
    Thanks
    25
    Thanked 0 Times in 0 Posts
    I see the logic to use this:
    $temp = $array[6];
    $array[6] = $array[4];
    $array[4] = $temp;

    I tried it before I asked for help, it is not working. It does not matter where in the script I add this.
    Right after <split> or in <foreach>/<for> loop. I see you know the secret place to add it.

    Also, if I use @array = split (/ /, $_) the print is terrible to read.
    So I end up using @array = split (/[:\s]/, $_

    Thanks for your help!


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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