using push

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyMarlboro
    New Member
    • Mar 2008
    • 71

    #1

    using push

    hi...
    i have problem in using push function.

    Code:
    @aa= qq (aa bb cc);
    push (@aa, 'dd');
    print Dumper (@aa);
    exit;
    result as follow:
    $VAR1 = 'aa bb cc';
    $VAR2 = 'dd';

    actually wat i wan is $VAR1 = 'aa bb cc dd';
    i knew that i could archieve it through
    Code:
    @aa= qq (aa bb cc);
    push (@aa, 'dd');
    @aa=qq(@aa);
    print Dumper (@aa);
    exit;
    $VAR1 = 'aa bb cc dd';


    but i don wan to use the qq function.. is there any method to 'push' the new entry to the back of array and not the new line?

    Thanks
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Use the qw operator.

    [CODE=perl]my @array = qw(aa bb cc);
    push @array, 'dd';
    print Dumper(\@array) ;[/CODE]
    Code:
    $VAR1 = [
              'aa',
              'bb',
              'cc',
              'dd'
            ];
    --Kevin

    Comment

    • MyMarlboro
      New Member
      • Mar 2008
      • 71

      #3
      Originally posted by eWish
      Use the qw operator.

      [CODE=perl]my @array = qw(aa bb cc);
      push @array, 'dd';
      print Dumper(\@array) ;[/CODE]
      Code:
      $VAR1 = [
                'aa',
                'bb',
                'cc',
                'dd'
              ];
      --Kevin

      emm... not tat i wan. sorry as i didnt give a good example.
      the question is how to add an item to the end back of an array?
      looking at the push function, it will add to the new line of the array.
      please do tell me if my statement is not clear.
      thx

      Comment

      • eWish
        Recognized Expert Contributor
        • Jul 2007
        • 973

        #4
        What I posted was adding the 'dd' to the end of the array. When you use the qq{} operator, it is simply a double quoted string which means that @array= qq{aa bb cc} has only one element 'aa bb cc', not an array with 3 elements.

        [CODE=perl]my @array = qw(aa bb cc);
        push @array,'dd';

        print "First Element: $array[0] \n"; # prints aa
        print "Second Element: $array[1] \n"; # prints bb
        print "Thrid Element: $array[2] \n"; # prints cc
        print "Fourth Element: $array[3] \n"; # prints dd which means that dd was added to the end or back of the array.[/CODE]
        [CODE=perl]
        my @array2 = qq{aa bb cc};
        print $array2[0]; # returns aa bb cc which is the only element in the array.[/CODE]

        The code I posted above added the 'dd' to the end of the array and using the qw{} operator defines the array as three seperate elements.

        --Kevin

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          push does add the new element to the end of the array, the problem is 'qq' as eWish has already noted. You should also a pass a reference to the data when you use Data::Dumper to print a data structure:

          print Dumper \@array;

          otherwise each element of the data structure is printed as a seperate VAR.

          Comment

          • MyMarlboro
            New Member
            • Mar 2008
            • 71

            #6
            thanks for inputs.

            I still confused.
            let take an example as below:

            Code:
            @aa= ("Helo");
            push(@aa, ",how are you");
            
            print Dumper (@aa);
            exit;
            what i got is :
            $VAR1 = 'Helo';
            $VAR2 = ',how are you';


            instead i would like to get
            $VAR1 = 'Helo,how are you'';

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Then use a string instead of an array.

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Also, Data::Dumper is what is causing the array to display like that, if you did not use Data::Dumper there would be no newlines in the display:

                Code:
                @aa= ("Helo");
                push(@aa, ",how are you");
                print @aa;

                Comment

                • MyMarlboro
                  New Member
                  • Mar 2008
                  • 71

                  #9
                  thanks for the input... i give another example:

                  Code:
                  for $j(1...10){
                  if ($j== 5){
                  print "$j\n";
                  }
                  else {
                  print "$j\,";
                  }
                  }
                  exit;
                  where i could get output as
                  1,2,3,4,5
                  6,7,8,9,10,

                  because i would like to store the data to array first...
                  Code:
                  for $j(1...10){
                  if ($j== 5){
                  #print "$j\n";
                  push (@aa, $j);
                  }
                  else {
                  #print "$j\,";
                  push (@aa, join(",",$j,""));
                  }
                  }
                  
                  print Dumper (@aa);
                  exit;
                  where i get the output as
                  $VAR1 = '1,';
                  $VAR2 = '2,';
                  $VAR3 = '3,';
                  $VAR4 = '4,';
                  $VAR5 = 5;
                  $VAR6 = '6,';
                  $VAR7 = '7,';
                  $VAR8 = '8,';
                  $VAR9 = '9,';
                  $VAR10 = '10,';


                  _______________ ___
                  i hope i could get
                  $VAR1 = '1,2,3,4,5';
                  $VAR2 = '6,7,8,9,10,';

                  Comment

                  • nithinpes
                    Recognized Expert Contributor
                    • Dec 2007
                    • 410

                    #10
                    For that, you need to push a newline into array along with the variable when its value becomes 5.
                    Code:
                    for $j(1...10){
                    if ($j==5)
                    {
                    push (@aa, "$j\n");
                    }
                    else {
                    push (@aa, join(",",$j,""));
                    }
                    }
                    
                    print @aa;
                    Other than inserting newline after 5, if you want to have a newline after every multiple of 5 (5,10,15,...). Replace,
                    Code:
                    if ($j==5)
                    with:

                    Code:
                    if ($j%5==0)
                    Last edited by nithinpes; Mar 14 '08, 07:33 AM. Reason: corrected typo

                    Comment

                    • MyMarlboro
                      New Member
                      • Mar 2008
                      • 71

                      #11
                      Originally posted by nithinpes
                      For that, you need to push a newline into array along with the variable when its value becomes 5.
                      Code:
                      for $j(1...10){
                      if ($j==5)
                      {
                      push (@aa, "$j\n");
                      }
                      else {
                      push (@aa, join(",",$j,""));
                      }
                      }
                      
                      print @aa;
                      Other than inserting newline after 5, if you want to have a newline after every multiple of 5 (5,10,15,...). Replace,
                      Code:
                      if ($j==5)
                      with:

                      Code:
                      if ($j%5==0)

                      emm... please refer to my post #9.
                      Again. i wish to say that i want to get (using the print Dumper)
                      $VAR1 = '1,2,3,4,5';
                      $VAR2 = '6,7,8,9,10';

                      thanks

                      Comment

                      • nithinpes
                        Recognized Expert Contributor
                        • Dec 2007
                        • 410

                        #12
                        If you are using Data::Dumper, each element of the array will be printed in new line. For your desired output, you should create an array with two elements "1,2,3,4,5" and "6,7,8,9,10 ".

                        Try this:
                        [CODE=perl]
                        use Data::Dumper;
                        $str="";
                        for $j(1...10){
                        if($j%5==0)
                        { push @aa,$str.$j;
                        $str=""; ###reinitialize to null
                        }
                        else {
                        $str.= $j.","; ## concatenate string with "," as delimiter
                        }
                        }
                        print Dumper (@aa);
                        exit;
                        [/CODE]
                        Last edited by nithinpes; Mar 14 '08, 09:36 AM. Reason: added explanation

                        Comment

                        • MyMarlboro
                          New Member
                          • Mar 2008
                          • 71

                          #13
                          Originally posted by nithinpes
                          If you are using Data::Dumper, each element of the array will be printed in new line. For your desired output, you should create an array with two elements "1,2,3,4,5" and "6,7,8,9,10 ".

                          Try this:
                          [CODE=perl]
                          use Data::Dumper;
                          $str="";
                          for $j(1...10){
                          if($j%5==0)
                          { push @aa,$str.$j;
                          $str=""; ###reinitialize to null
                          }
                          else {
                          $str.= $j.","; ## concatenate string with "," as delimiter
                          }
                          }
                          print Dumper (@aa);
                          exit;
                          [/CODE]
                          thank you so much. Good day.
                          thx

                          Comment

                          Working...