Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6075007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:26:47+00:00 2026-05-23T10:26:47+00:00

I have the following test script /^[^a-zA-Z0-9]/ { DATEd[$3] = $1 } END {

  • 0

I have the following test script

 /^[^a-zA-Z0-9]/  {
    DATEd[$3] = $1
    } 
   END { 
        print "        \"data\": ["
        for (i = 0 ; i <= 5; i ++ ) {
            { print "            [" i ", \"" DATEd[i] "\"],"}
        }
        print "        ]"
}

And are reading from this text file

2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399
2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399 
2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399 
2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399
2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399 
2011-01-22 22:12 P16A22_110114072915 22 1312 75 13.55 1399

But it doesn’t print out what I want it to, I want it to print out

    "data": [
        [0, "2011-01-22"],
        [1, "2011-01-22"],
        [2, "2011-01-22"],
        [3, "2011-01-22"],
        [4, "2011-01-22"],
        [5, "2011-01-22"],
    ]

When it in fact are only printing out

"data": [
    [0, ""],
    [1, ""],
    [2, ""],
    [3, ""],
    [4, ""],
    [5, ""],
]

So why is “DATEd[$3] = $1” empty?

Also how do I check the length of an array?
DATEd.length doesn’t work in this case.

Thanks

EDIT_______________________________________________

So from the help of @Fredrik and @geekosaur I have come somewhere with this, now to some last questions

1) The script now looks like this

 /[a-zA-Z0-9]/  {
    DATEd[NR-1] = $1
    } 
   END { 
        print "        \"data\": ["

        for (i in DATEd) {
            { print "            [" i ", \"" DATEd[i] "\"],"}
        }
        print "        ]"
}

And gives the following output

"data": [
    [4, "2011-01-26"],
    [5, "2011-01-27"],
    [6, "2011-01-28"],
    [0, "2011-01-22"],
    [1, "2011-01-23"],
    [2, "2011-01-24"],
    [3, "2011-01-25"],
]

But I want it to look like this

"data": [
[0, "2011-01-22"],
[1, "2011-01-23"],
[2, "2011-01-24"],
[3, "2011-01-25"],
[4, "2011-01-26"],
[5, "2011-01-27"],
[6, "2011-01-28"]
]

I.E be sorted and removing the last ‘,’ character before the final closing ‘]’ character. Is this possible to accieve in a easy way? =)

Thanks =)

EDIT 3 Final Outcome_______________________________________

Used a combination of @geekosaur and @Fredrik contribution’s =)

{
    DATEd[NR-1] = $1; len++
}
   END { 
        print "        \"data\": ["

        #for (i in DATEd) {
        for (i = 0 ; i <= len-1; i ++ ) {
            { print "            [" i ", \"" DATEd[i] "\"],"}
        }
        print "        ]"
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:26:47+00:00Added an answer on May 23, 2026 at 10:26 am

    As a start, your regex is wrong, /^[^a-zA-Z0-9]/ means to match the start of a line and NOT followed by a letter or a number. None of the lines have that setup, hence, your array DATe is empty.

    Secondly, your array is not indexed by 0-5 but instead the content of $3 (if you fix your regex)

    There is no built in function to get the length of an array, but it’s simple to implement one.

    Array example

    function array_length(a) {
        for (i in a) n++
        return n
    }
    
    {
        DATEd[NR] = $1
    }
    END {
        for (i in DATEd) {
            print i, DATEd[i]
        }
        print "Number of items", array_length(DATEd)
    
        # copy indices
        j = 1
        for (i in DATEd) {
            ind[j] = i    # index value becomes element value
            j++
        }
        n = asort(ind)    # index values are now sorted
        for (i = 1; i <= n; i++)
            print i, DATEd[ind[i]]
    }
    

    Gives:

    4 2011-01-22
    5 2011-01-22
    6 2011-01-22
    1 2011-01-22
    2 2011-01-22
    3 2011-01-22
    Number of items 6
    1 2011-01-22
    2 2011-01-22
    3 2011-01-22
    4 2011-01-22
    5 2011-01-22
    6 2011-01-22
    

    See the gnu awk manual for an description of arrays

    Too loop through all elements of an array, use this construct (see link above)

     for (var in array)
       body
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For simplicity, i have the following file named test.jsp: <script language=javascript> alert(a$b.replace(/\$/g,k)); </script> I
I have the following code to test the menu button : <script type=text/javascript cahrset=utf-8>
I have a page, test.php, with the following code: <html> <body> <form> <script type=text/javascript>
I have the following test ASPX page: <head runat=server> <title></title> <script src=js/jquery-1.2.6.min.js type=text/javascript></script> <script
I have taken following code from my template file, <a onclick=previewPop('<?= $this->url(array('controller'=>'report', 'action'=>'generatepreview','report_date'=>str_replace(%2F, /,
I have the following simple python test script that uses Suds to call a
I have the following test code use Data::Dumper; my $hash = { foo =>
I have the following dummy test script: function test() { var x = 0.1
I have the following test defined in a psake build script: task package -depends
I have been following this link Android kernel compile and test with Android Emulator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.